<?php
/**
 * PHP HTClient Connection class
 *
 * A connection, data retrieving and management class for the on-line
 * game Hattrick by Extralives AB (http://www.hattrick.org/).
 *
 * Released under terms of the GNU Lesser General Public License [LGPL]
 * (http://www.opensource.org/licenses/lgpl-license.html).
 *
 * @author Davide Lucchesi <davide@hattrickhq.org>
 * @version 1.0 $
 * @package HTClient
 */

/** To use HTClient, you need also the Advanced HTTP Client Class, that is
 * available for free visiting phpclasses (http://www.phpclasses.org/).
 */
require_once 'http.inc';

define('HTCLIENT_VERSION', '1.0');
define('HT_DEFAULT', 'www.hattrick.org');
define('HT_PARAMS', '?outputType=XML');

define('HT_START', '/Common/menu.asp');
define('HT_HOME', '/Common/default.asp');
define('HT_MATCH', '/Common/matchDetails.asp');
define('HT_MATCHLINEUP', '/Common/matchLineup.asp');
define('HT_TEAM', '/Common/teamDetails.asp');
define('HT_CLUB', '/Common/club.asp');
define('HT_ECONOMY', '/Common/economy.asp');
define('HT_PLAYERS', '/Common/players.asp');
define('HT_PLAYER', '/Common/playerDetails.asp');
define('HT_REGION', '/Common/regionDetails.asp');
define('HT_WORLD', '/Common/worldDetails.asp');
define('HT_TRAINING', '/Common/training.asp');
define('HT_SERIES', '/Common/leagueDetails.asp');
define('HT_TRANSFERS', '/Common/transferHistory.asp');
define('HT_MATCHES', '/Common/matches.asp');
define('HT_MATCHESARCHIVE', '/Common/matchesArchive.asp');
define('HT_FIXTURES', '/Common/leagueFixtures.asp');
define('HT_LIVE', '/Common/live.asp');

define('TLEN_STAMINA', 1);
define('TLEN_KEEPER', 5);
define('TLEN_PLAYMAKER', 7);
define('TLEN_SCORER', 7);
define('TLEN_PASSING', 5);
define('TLEN_WINGER', 5);
define('TLEN_DEFENDER', 8);
define('TLEN_SETPIECES', 2);

define('T_GENERAL', 0);
define('T_STAMINA', 1);
define('T_SETPIECES', 2);
define('T_DEFENDING', 3);
define('T_SCORING', 4);
define('T_WINGER', 5);
define('T_SHOOTING', 6);
define('T_SHORTPASSES', 7);
define('T_PLAYMAKER', 8);
define('T_GOALTENDING', 9);
define('T_THROUGHPASSES', 10);
define('T_DEFENSIVEPOSITIONS', 11);
define('T_WINGATTACKS', 12);

/**
 * PHP HTClient Connection class
 *
 * Within the HTClient class, we could distinguish two different groups
 * of public functions.
 *
 * ==> Group 1: Connection/Information retrieving
 *
 * Here you can find needed functions to login, logout and fetch
 * information from the Hattrick servers.
 * For every Hattrick available resource (XML file) you can find two or
 * even three different functions, with the following name format:
 *
 * - getSomething()
 * will compose an associative array with all the useful data fetched
 * from the suitable XML file.
 * You will mainly call this kind of funcions and let the HTClient class
 * automatically manage the parsing and caching of this information.
 * Caching is very useful to reduce the network overload and to boost
 * the speed of your application on consecutive calls of the same
 * functions.
 *
 * - getSomethingXML()
 * will simply retrieve and return the XML file as a string.
 *
 * - clearSomething()
 * will remove the previous fetched information from the cache.
 *
 * Functions for available XMLs:
 *
 * /Common/menu.asp and /Common/default.asp
 * - HTClient($agent) [constructor]
 * - setHost($host)
 * - getHost()
 * - checkSecurityCode($username)
 * - login($username, $password)
 * - logout()
 *
 * /Common/club.asp
 * - clearClub()
 * - getClubXML()
 * - getClub()
 *
 * /Common/teamDetails.asp
 * - clearTeamDetails()
 * - getTeamDetailsXML($id = false) [with the fix from CHPP-ste1n]
 * - getTeamDetails($id = false)
 *
 * /Common/economy.asp
 * - clearEconomy()
 * - getEconomyXML()
 * - getEconomy()
 *
 * /Common/worldDetails.asp
 * - getWorldDetailsXML($type = 'leagues') [Courtesy of CHPP-ste1n]
 * - getWorldDetails($type = 'leagues', $fix = false) [with the fix from CHPP-ste1n]
 *
 * /Common/regionDetails.asp
 * - clearRegionDetails()
 * - getRegionDetailsXML ($id = false) [Courtesy of CHPP-ste1n]
 * - getRegionDetails($id = false)
 *
 * /Common/leagueDetails.asp
 * - clearLeagueDetails()
 * - getLeagueDetailsXML($id = false) [Courtesy of CHPP-ste1n]
 * - getLeagueDetails($id = false)
 *
 * /Common/matches.asp
 * - clearMatches()
 * - getMatchesXML($id = false)
 * - getMatches($id = false)
 *
 * /Common/matchesArchive.asp
 * - clearMatchesArchive()
 * - getMatchesArchiveXML($id = false, $cup = false, $first = false, $last = false, $season = false) [suggested by CyberKieken]
 * - getMatchesArchive($id = false, $cup = false, $first = false, $last = false, $season = false)
 *
 * /Common/leagueFixtures.asp
 * - clearLeagueFixtures()
 * - getLeagueFixturesXML($id = false, $season = false)
 * - getLeagueFixtures($id = false, $season = false)
 *
 * /Common/matchDetails.asp
 * - getMatchXML($id)
 * - getMatch($id = false)
 *
 * /Common/matchLineup.asp
 * - getMatchLineupXML($matchid = false, $teamid = false)
 * - getMatchLineup($matchid = false, $teamid = false)
 *
 * /Common/live.asp
 * - getLiveMatchXML($id)
 * - getLiveMatch($id)
 *
 * /Common/training.asp
 * - clearTraining()
 * - getTrainingXML() [Courtesy of CHPP-ste1n]
 * - getTraining()
 * - clearTrainingStats()
 * - getTrainingStatsXML($id = false) [Courtesy of CHPP-ste1n]
 * - getTrainingStats($id = false)
 *
 * /Common/players.asp
 * - clearPlayers()
 * - getPlayersXML($id = false)
 * - getPlayers($id = false)
 *
 * /Common/playerDetails.asp
 * - getPlayerXML($id)
 * - getPlayer($id)
 *
 * /Common/transferHistory.asp
 * - clearTransferHistory()
 * - getTransferHistoryXML($id, $type = 'player')
 * - getTransferHistory($id, $type = 'player')
 *
 *
 * ==> Group 2: Utility functions
 *
 * There are many duties that could be achieved directly by the HTClient
 * class, like evaluating a player in every role or computing sub-skill
 * evaluations based on trainings that a player received.
 * Please, read provided details for every function to see how many
 * features are ready to suite your application needs.
 *
 * Available functions:
 *
 * - splitPlayerNames($players, $max = false)
 * - isTrained($code, $type)
 * - getTrainingWeeks($age, $level, $tr = 7, $kt = 10, $at = 10)
 * - evaluateTraining($player, $level, $tr = 7, $kt = 10, $at = 10)
 * - evaluateTrainings($players, $level, $tr = 7, $kt = 10, $at = 10)
 * - setTraining($player, $skill, $value = 0)
 * - addTraining($player, $skill, $value = 0)
 * - evaluatePlayer($player)
 * - evaluatePlayers($players)
 * - isResigned($playerid)
 * - getMissingPlayers($players)
 * - getChanges($players, $fix = false)
 * - getNewPlayers($players)
 * - getPlayersItem($val, $players = false, $field = 'PLAYERID')
 * - getLeagueItem($val, $league = false, $field = 'TEAMID')
 * - getWorldItem($val, $world = false, $field = 'LEAGUEID')
 * - getMatchesItem($val, $matches = false, $field = 'MATCHID')
 * - fillupMatches($matches)
 * - insertPlayerImages($player_text) [Courtesy of Wolfgang Schedl]
 * - teamid2leagueid($teamid) [Courtesy of CHPP-ste1n]
 * - getFlags($teamid = false) [Courtesy of CHPP-ste1n]
 *
 * @package HTClient
 * @author Davide Lucchesi <davide@hattrickhq.org>
 * @version 1.0 $
 */
class HTClient
{
/**
 * An Advanced HTTP Client class instance
 * @var object
 */
var $http;

/**
 * Suggested Hattrick server
 * @var string
 */
var $host;

/**
 * Club information
 * @var array
 */
var $club;

/**
 * Economy information
 * @var array
 */
var $economy;

/**
 * League information
 * @var array
 */
var $league;

/**
 * Team Detail information
 * @var array
 */
var $teamDetails;

/**
 * Players list
 * @var array
 */
var $players;

/**
 * Missing players list
 * @var array
 */
var $playersMissing;

/**
 * New players list
 * @var array
 */
var $playersFound;

/**
 * Training
 * @var array
 */
var $training;

/**
 * Training Stats
 * @var array
 */
var $trainingStats;

/**
 * Region Details information
 * @var array
 */
var $regionDetails;

/**
 * World Detail Leagues information
 * @var array
 */
var $worldLeagues;

/**
 * World Detail Languages information
 * @var array
 */
var $worldLanguages;

/**
 * Transfer History information
 * @var array
 */
var $transfer;

/**
 * Matches information
 * @var array
 */
var $matches;

/**
 * Matches archive information
 * @var array
 */
var $matchesArchive;

/**
 * League fixtures information
 * @var array
 */
var $fixtures;


/* ==> Group 1: Connection/Information retrieving */

/**
 * Constructor
 *
 * @access   public
 * @param    string   User agent (client application name)
 */
function HTClient($agent)
{
	$this->http = &new http(HTTP_V11, false);
	$this->http->user_agent = $agent;
	$this->host = false;
	$this->club = false;
	$this->economy = false;
	$this->league = false;
	$this->teamDetails = false;
	$this->players = false;
	$this->playersMissing = false;
	$this->playersFound = false;
	$this->training = false;
	$this->trainingStats = false;
	$this->regionDetails = false;
	$this->worldLeagues = false;
	$this->worldLanguages = false;
	$this->transfer = false;
	$this->matches = false;
	$this->matchesArchive = false;
	$this->fixtures = false;
}

/**
 * Set the reference http server.
 *
 * @access   public
 * @param    string   Hostname
 */
function setHost($host)
{
	$this->http->host = $host;
}

/**
 * Get reference http server parsing suggested URL from Hattrick.
 *
 * @access   public
 */
function getHost()
{
	if ($this->http->host)
		return $this->http->host;
	$this->http->host = HT_DEFAULT;
	$code = $this->http->get(HT_START.'?outputType=XML');
	if ($code != HTTP_STATUS_OK)
		return false;
	$response = $this->http->get_response_body();
	$parser = xml_parser_create();
	xml_parse_into_struct($parser, $response, $vals, $index);
	xml_parser_free($parser);
	$this->http->host = str_replace( "http://", "", $vals[$index['RECOMMENDEDURL'][0]]['value'] );
	return $this->http->host;
}

/**
 * Check user's Security Code.
 *
 * @access   public
 * @param    string   Username
 */
function checkSecurityCode($username)
{
	$this->getHost();
	$form = array('loginname' => $username,
		'actionType' => 'checkSecurityCode',
		'outputType' =>  'XML'
	);
	$code = $this->http->post(HT_HOME, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code == HTTP_STATUS_OK) {
		$response = $this->http->get_response_body();
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $response, $vals, $index);
		xml_parser_free($parser);
		if ($vals[$index['ACTIONSUCCESSFUL'][0]]['value'] == 'True')
			return $vals[$index['FETCHEDDATE'][0]]['value'];
		else return false;
	}
	else return false;
}

/**
 * Login to an Hattrick server.
 *
 * @access   public
 * @param    string   Username
 * @param    string   Read-Only password (security code)
 */
function login($username, $password)
{
	$this->getHost();
	$form = array('loginname' => $username,
		'readonlypassword' => $password,
		'loginType' => 'CHPP',
		'actionType' => 'login',
		'outputType' =>  'XML'
	);
	$code = $this->http->post(HT_HOME, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code == HTTP_STATUS_OK)
		return $this->http->host;
	else return false;
}

/**
 * Close Hattrick session.
 *
 * @access   public
 */
function logout()
{
	$form = array('actionType' => 'logout');
	$code = $this->http->post(HT_HOME, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code == HTTP_STATUS_OK)
		return true;
	else return false;
}

/**
 * Remove cached information about team staff.
 *
 * @access   public
 */
function clearClub()
{
	$this->club = false;
}

/**
 * Retrieve information about team staff.
 *
 * @access   public
 */
function getClubXML()
{
	$form = array('outputType' => 'XML',
		'actionType' => 'view'
	);
	$code = $this->http->post(HT_CLUB, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve information about team staff.
 *
 * @access   public
 */
function getClub()
{
	if ($this->club)
		return $this->club;
	$xml = $this->getClubXML();
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		$this->club = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'KEEPERTRAINERS' => $vals[$index['KEEPERTRAINERS'][0]]['value'],
			'ASSISTANTTRAINERS' => $vals[$index['ASSISTANTTRAINERS'][0]]['value'],
			'PSYCHOLOGISTS' => $vals[$index['PSYCHOLOGISTS'][0]]['value'],
			'PRESSSPOKESMEN' => $vals[$index['PRESSSPOKESMEN'][0]]['value'],
			'ECONOMISTS' => $vals[$index['ECONOMISTS'][0]]['value'],
			'PHYSIOTHERAPISTS' => $vals[$index['PHYSIOTHERAPISTS'][0]]['value'],
			'DOCTORS' => $vals[$index['DOCTORS'][0]]['value'],
			'INVESTMENT' => $vals[$index['INVESTMENT'][0]]['value'],
			'HASPROMOTED' => $vals[$index['HASPROMOTED'][0]]['value'],
			'YOUTHLEVEL' => $vals[$index['YOUTHLEVEL'][0]]['value']
		);
		return $this->club;
	}
	else return false;
}

/**
 * Remove cached information about a team.
 *
 * @access   public
 */
function clearTeamDetails()
{
	$this->teamDetails = false;
}

/**
 * Retrieve information about specified team (or user's one).
 *
 * @access   public
 * @param    string   TeamID
 */
function getTeamDetailsXML($id = false)
{
	if ($id)
		$form = array('outputType' => 'XML',
			'actionType' => 'view',
			'teamID' => $id
		);
	else
		$form = array('outputType' => 'XML',
			'actionType' => 'view'
		);
	$code = $this->http->post(HT_TEAM, $form, 'http://'.$this->http->host.HT_HOME);

	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve information about specified team (or user's one).
 *
 * @access   public
 * @param    string   TeamID
 */
function getTeamDetails($id = false)
{
	if ((! $id) && ($this->teamDetails))
		return $this->teamDetails;
	$xml = $this->getTeamDetailsXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		$this->teamDetails = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'LANGUAGEID' => $vals[$index['LAUNGUAGEID'][0]]['value'],
			'LANGUAGENAME' => $vals[$index['LANGUAGENAME'][0]]['value'],
			'HASSUPPORTER' => $vals[$index['HASSUPPORTER'][0]]['value'],
			'LOGINNAME' => $vals[$index['LOGINNAME'][0]]['value'],
			'EMAIL' => $vals[$index['EMAIL'][0]]['value'],
			'NAME' => $vals[$index['NAME'][0]]['value'],
			'ICQ' => $vals[$index['ICQ'][0]]['value'],
			'SIGNUPDATE' => $vals[$index['SIGNUPDATE'][0]]['value'],
			'ACTIVATIONDATE' => $vals[$index['ACTIVATIONDATE'][0]]['value'],
			'LASTLOGINDATE' => $vals[$index['LASTLOGINDATE'][0]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'SHORTTEAMNAME' => $vals[$index['SHORTTEAMNAME'][0]]['value'],
			'ARENAID' => $vals[$index['ARENAID'][0]]['value'],
			'ARENANAME' => $vals[$index['ARENANAME'][0]]['value'],
			'LEAGUEID' => $vals[$index['LEAGUEID'][0]]['value'],
			'LEAGUENAME' => $vals[$index['LEAGUENAME'][0]]['value'],
			'REGIONID' => $vals[$index['REGIONID'][0]]['value'],
			'REGIONNAME' => $vals[$index['REGIONNAME'][0]]['value'],
			'PLAYERID' => $vals[$index['PLAYERID'][0]]['value'], // trainer
			'PLAYERNAME' => $vals[$index['PLAYERNAME'][0]]['value'], // trainer
			'HOMEPAGE' => $vals[$index['HOMEPAGE'][0]]['value'],
			'DRESS' => $vals[$index['DRESS'][0]]['value'],
			'DRESSALTERNATE' => $vals[$index['DRESSALTERNATE'][0]]['value'],
			'LEAGUELEVELUNITID' => $vals[$index['LEAGUELEVELUNITID'][0]]['value'],
			'LEAGUELEVELUNITNAME' => $vals[$index['LEAGUELEVELUNITNAME'][0]]['value'],
			'LEAGUELEVEL' => $vals[$index['LEAGUELEVEL'][0]]['value'],
			'ISBOT' => $vals[$index['ISBOT'][0]]['value'],
			'BOTSINCE' => $vals[$index['BOTSINCE'][0]]['value'],
			'STILLINCUP' => $vals[$index['STILLINCUP'][0]]['value'],
			'CUPID' => $vals[$index['CUPID'][0]]['value'],
			'CUPNAME' => $vals[$index['CUPNAME'][0]]['value'],
			'FRIENDLYBOOKED' => $vals[$index['FRIENDLYBOOKED'][0]]['value'],
			'FRIENDLYTEAMID' => $vals[$index['FRIENDLYTEAMID'][0]]['value'],
			'NUMBEROFVICTORIES' => $vals[$index['NUMBEROFVICTORIES'][0]]['value'],
			'NUMBEROFUNDEFEATED' => $vals[$index['NUMBEROFUNDEFEATED'][0]]['value'],
			'TEAMRANK' => $vals[$index['TEAMRANK'][0]]['value'],
			'LOGOURL' => $vals[$index['LOGOURL'][0]]['value'],
			'FANCLUBID' => $vals[$index['FANCLUBID'][0]]['value'],
			'FANCLUBNAME' => $vals[$index['FANCLUBNAME'][0]]['value'],
			'NUMBEROFGUESTBOOKITEMS' => $vals[$index['NUMBEROFGUESTBOOKITEMS'][0]]['value'],
			'SUBJECT' => $vals[$index['SUBJECT'][0]]['value'],
			'BODY' => $vals[$index['BODY'][0]]['value'],
			'SENDDATE' => $vals[$index['SENDDATE'][0]]['value']
		);
		return $this->teamDetails;
	}
	else return false;
}

/**
 * Remove cached information about team economy.
 *
 * @access   public
 */
function clearEconomy()
{
	$this->economy = false;
}

/**
 * Retrieve information about team economy.
 *
 * @access   public
 */
function getEconomyXML()
{
	$form = array('outputType' => 'XML',
		'actionType' => 'view'
	);
	$code = $this->http->post(HT_ECONOMY, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve information about team economy.
 *
 * @access   public
 */
function getEconomy()
{
	if ($this->economy)
		return $this->economy;
	$xml = $this->getEconomyXML();
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		$this->economy = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'CASH' => $vals[$index['CASH'][0]]['value'],
			'EXPECTEDCASH' => $vals[$index['EXPECTEDCASH'][0]]['value'],
			'SPONSORSPOPULARITY' => $vals[$index['SPONSORSPOPULARITY'][0]]['value'],
			'SUPPORTERSPOPULARITY' => $vals[$index['SUPPORTERSPOPULARITY'][0]]['value'],
			'FANCLUBSIZE' => $vals[$index['FANCLUBSIZE'][0]]['value'],
			'INCOMESPECTATORS' => $vals[$index['INCOMESPECTATORS'][0]]['value'],
			'INCOMESPONSORS' => $vals[$index['INCOMESPONSORS'][0]]['value'],
			'INCOMEFINANCIAL' => $vals[$index['INCOMEFINANCIAL'][0]]['value'],
			'INCOMETEMPORARY' => $vals[$index['INCOMETEMPORARY'][0]]['value'],
			'INCOMESUM' => $vals[$index['INCOMESUM'][0]]['value'],
			'COSTSARENA' => $vals[$index['COSTSARENA'][0]]['value'],
			'COSTSPLAYERS' => $vals[$index['COSTSPLAYERS'][0]]['value'],
			'COSTSFINANCIAL' => $vals[$index['COSTSFINANCIAL'][0]]['value'],
			'COSTSTEMPORARY' => $vals[$index['COSTSTEMPORARY'][0]]['value'],
			'COSTSSTAFF' => $vals[$index['COSTSSTAFF'][0]]['value'],
			'COSTSYOUTH' => $vals[$index['COSTSYOUTH'][0]]['value'],
			'COSTSSUM' => $vals[$index['COSTSSUM'][0]]['value'],
			'EXPECTEDWEEKSTOTAL' => $vals[$index['LASTEXPECTEDWEEKSTOTAL'][0]]['value'],
			'LASTINCOMESPECTATORS' => $vals[$index['LASTINCOMESPECTATORS'][0]]['value'],
			'LASTINCOMESPONSORS' => $vals[$index['LASTINCOMESPONSORS'][0]]['value'],
			'LASTINCOMEFINANCIAL' => $vals[$index['LASTINCOMEFINANCIAL'][0]]['value'],
			'LASTINCOMETEMPORARY' => $vals[$index['LASTINCOMETEMPORARY'][0]]['value'],
			'LASTINCOMESUM' => $vals[$index['LASTINCOMESUM'][0]]['value'],
			'LASTCOSTSARENA' => $vals[$index['LASTCOSTSARENA'][0]]['value'],
			'LASTCOSTSPLAYERS' => $vals[$index['LASTCOSTSPLAYERS'][0]]['value'],
			'LASTCOSTSFINANCIAL' => $vals[$index['LASTCOSTSFINANCIAL'][0]]['value'],
			'LASTCOSTSTEMPORARY' => $vals[$index['LASTCOSTSTEMPORARY'][0]]['value'],
			'LASTCOSTSSTAFF' => $vals[$index['LASTCOSTSSTAFF'][0]]['value'],
			'LASTCOSTSYOUTH' => $vals[$index['LASTCOSTSYOUTH'][0]]['value'],
			'LASTCOSTSSUM' => $vals[$index['LASTCOSTSSUM'][0]]['value'],
			'LASTWEEKSTOTAL' => $vals[$index['LASTWEEKSTOTAL'][0]]['value']
		);
		return $this->economy;
	}
	else return false;
}

/**
 * Retrieve world details XML.
 * If type = languages, retrieve list of languages;
 * otherwise, retrieve league info.
 *
 * @param    string   actionType
 * @access   public
 */
function getWorldDetailsXML($type = 'leagues')
{
	if ($type == 'languages')
		$form = array('outputType' => 'XML',
			'actionType' => 'languages',
		);
	else
		$form = array('outputType' => 'XML',
			'actionType' => 'leagues'
		);
	$code = $this->http->post(HT_WORLD, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve world details.
 *
 * @param    string   actionType
 * @param    boolean  Fix wrong season numbers
 * @access   public
 */
function getWorldDetails($type = 'leagues', $fix = false)
{
	$xml = $this->getWorldDetailsXML($type);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		if ($type == 'languages') {
			for ($i = 0; $i < count($index['LANGUAGEID']); $i++) {
				$language[$i] = array(
					'LANGUAGEID' => $vals[$index['LANGUAGEID'][$i]]['value'],
					'LANGUAGENAME' => $vals[$index['LANGUAGENAME'][$i]]['value']
				);
			}
			$this->worldLanguages = array(
				'FILENAME'		=> $vals[$index['FILENAME'][0]]['value'],
				'VERSION'		=> $vals[$index['VERSION'][0]]['value'],
				'USERID'		=> $vals[$index['USERID'][0]]['value'],
				'FETCHEDDATE'		=> $vals[$index['FETCHEDDATE'][0]]['value'],
				'LANGUAGE' => $language
			);
			return $this->worldLanguages;
		}
		else {
			for ($i = 0; $i < count($index['LEAGUEID']); $i++) {
				$league[$i] = array(
					'LEAGUEID' => $vals[$index['LEAGUEID'][$i]]['value'],
					'LEAGUENAME' => $vals[$index['LEAGUENAME'][$i]]['value'],
					'SEASON' => $vals[$index['SEASON'][$i]]['value'],
					'MATCHROUND' => $vals[$index['MATCHROUND'][$i]]['value'],
					'SHORTNAME' => $vals[$index['SHORTNAME'][$i]]['value'],
					'CONTINENT' => $vals[$index['CONTINENT'][$i]]['value'],
					'ZONENAME' => $vals[$index['ZONENAME'][$i]]['value'],
					'ENGLISHNAME' => $vals[$index['ENGLISHNAME'][$i]]['value'],
					'COUNTRYID' => $vals[$index['COUNTRYID'][$i]]['value'],
					'COUNTRYNAME' => $vals[$index['COUNTRYNAME'][$i]]['value'],
					'CURRENCYNAME' => $vals[$index['CURRENCYNAME'][$i]]['value'],
					'CURRENCYRATE' => $vals[$index['CURRENCYRATE'][$i]]['value'],
					'CUPID' => $vals[$index['CUPID'][$i]]['value'],
					'CUPNAME' => $vals[$index['CUPNAME'][$i]]['value'],
					'ACTIVEUSERS' => $vals[$index['ACTIVEUSERS'][$i]]['value'],
					'WAITINGUSERS' => $vals[$index['WAITINGUSERS'][$i]]['value'],
					'TRAININGDATE' => $vals[$index['TRAININGDATE'][$i]]['value'],
					'ECONOMYDATE' => $vals[$index['ECONOMYDATE'][$i]]['value'],
					'CUPMATCHDATE' => $vals[$index['CUPMATCHDATE'][$i]]['value'],
					'SERIESMATCHDATE' => $vals[$index['SERIESMATCHDATE'][$i]]['value'],
					'NUMBEROFLEVELS' => $vals[$index['NUMBEROFLEVELS'][$i]]['value']
				);
				if ($fix) {
					/* leagues that started in global season 11 are reported 10 seasons
					 * early, so:
					 */
					if ($league[$i]['LEAGUEID'] > 1 && $league[$i]['LEAGUEID'] < 9)
						$league[$i]['SEASON'] -= 10;
					/* also, Danmark and Finland are strange; they started in season 13
					 * but are reported as having started in 1 too:
					 */
					if ($league[$i]['LEAGUEID'] == 11 || $league[$i]['LEAGUEID'] == 12)
						$country[$i]['SEASON'] -= 12;
				}
			}
			$this->worldLeagues = array(
				'FILENAME'		=> $vals[$index['FILENAME'][0]]['value'],
				'VERSION'		=> $vals[$index['VERSION'][0]]['value'],
				'USERID'		=> $vals[$index['USERID'][0]]['value'],
				'FETCHEDDATE'		=> $vals[$index['FETCHEDDATE'][0]]['value'],
				'LEAGUE' => $league
			);
			return $this->worldLeagues;
		}
	}
	else return false;
}

/**
 * Remove cached region details.
 *
 * @access   public
 */
function clearRegionDetails()
{
	$this->regionDetails = false;
}

/**
 * Retrieve region Details XML.
 *
 * @param    string   RegionID
 * @access   public
 */
function getRegionDetailsXML ($id = false) {
	if ($id)
		$form = array('outputType' => 'XML',
			'actionType' => 'view',
			'RegionID' => $id
		);
	else
		$form = array('outputType' => 'XML',
			'actionType' => 'view'
		);
	$code = $this->http->post(HT_REGION, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve region details.
 *
 * @param    string   RegionID
 * @access   public
 */
function getRegionDetails($id = false)
{
	if ((! $id) && ($this->regionDetails))
		return $this->regionDetails;
	$xml = $this->getRegionDetailsXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		$this->regionDetails = array(
			'FILENAME'		=> $vals[$index['FILENAME'][0]]['value'],
			'VERSION'		=> $vals[$index['VERSION'][0]]['value'],
			'USERID'		=> $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE'		=> $vals[$index['FETCHEDDATE'][0]]['value'],
			'LEAGUEID'		=> $vals[$index['LEAGUEID'][0]]['value'],
			'LEAGUENAME'		=> $vals[$index['LEAGUENAME'][0]]['value'],
			'REGIONID'		=> $vals[$index['REGIONID'][0]]['value'],
			'REGIONNAME'		=> $vals[$index['REGIONNAME'][0]]['value'],
			'WEATHERID'		=> $vals[$index['WEATHERID'][0]]['value'],
			'TOMORROWWEATHERID'	=> $vals[$index['TOMORROWWEATHERID'][0]]['value'],
			'NUMBEROFUSERS'		=> $vals[$index['NUMBEROFUSERS'][0]]['value'],
			'NUMBEROFONLINE'	=> $vals[$index['NUMBEROFONLINE'][0]]['value']
		);
		return $this->regionDetails;
	}
	else return false;
}

/**
 * Remove cached information about league details.
 *
 * @access   public
 */
function clearLeagueDetails()
{
	$this->league = false;
}

/**
 * Retrieve series ('league') details XML for series $id, or for users
 * own series.
 *
 * @param    string   leagueLevelUnitID
 * @access   public
 */
function getLeagueDetailsXML($id = false) {
	if ($id)
		$form = array('outputType' => 'XML',
			'actionType' => 'view',
			'leagueLevelUnitID ' => $id
		);
	else
		$form = array('outputType' => 'XML',
			'actionType' => 'view'
		);
	$code = $this->http->post(HT_SERIES, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve series ('league') details for series $id, or for users own series.
 *
 * @param    string   leagueLevelUnitID
 * @access   public
 */
function getLeagueDetails($id = false) {
	if ((! $id) && ($this->league))
		return $this->league;
	$xml = $this->getLeagueDetailsXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['TEAMID']); $i++) {
			$team[$i] = array(
				'TEAMID' => $vals[$index['TEAMID'][$i]]['value'],
				'POSITION' => $vals[$index['POSITION'][$i]]['value'],
				'TEAMNAME' => $vals[$index['TEAMNAME'][$i]]['value'],
				'MATCHES' => $vals[$index['MATCHES'][$i]]['value'],
				'GOALSFOR' => $vals[$index['GOALSFOR'][$i]]['value'],
				'GOALSAGAINST' => $vals[$index['GOALSAGAINST'][$i]]['value'],
				'GoalsDiff' => $vals[$index['GOALSFOR'][$i]]['value'] - $vals[$index['GOALSAGAINST'][$i]]['value'],
				'POINTS' => $vals[$index['POINTS'][$i]]['value']
			);
		}
		$this->league = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'LEAGUEID' => $vals[$index['LEAGUEID'][0]]['value'],
			'LEAGUENAME' => $vals[$index['LEAGUENAME'][0]]['value'],
			'LEAGUELEVEL' => $vals[$index['LEAGUELEVEL'][0]]['value'],
			'MAXLEVEL' => $vals[$index['MAXLEVEL'][0]]['value'],
			'LEAGUELEVELUNITID' => $vals[$index['LEAGUELEVELUNITID'][0]]['value'],
			'LEAGUELEVELUNITNAME' => $vals[$index['LEAGUELEVELUNITNAME'][0]]['value'],
			'TEAM' => $team
		);
		return $this->league;
	}
	else return false;
}

/**
 * Remove cached information about matches.
 *
 * @access   public
 */
function clearMatches()
{
	$this->matches = false;
}

/**
 * Retrieve matches for team $id, or for user's own team.
 *
 * @param    string   teamID
 * @access   public
 */
function getMatchesXML($id = false) {
	if ($id)
		$form = array('outputType' => 'XML',
			'actionType' => 'view',
			'teamID ' => $id
		);
	else
		$form = array('outputType' => 'XML',
			'actionType' => 'view'
		);
	$code = $this->http->post(HT_MATCHES, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve matches for team $id, or for user's own team.
 *
 * @param    string   teamID
 * @access   public
 */
function getMatches($id = false) {
	if ((! $id) && ($this->matches))
		return $this->matches;
	$xml = $this->getMatchesXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['MATCHID']); $i++) {
			$match[$i] = array(
				'MATCHID' => $vals[$index['MATCHID'][$i]]['value'],
				'HOMETEAMID' => $vals[$index['HOMETEAMID'][$i]]['value'],
				'HOMETEAMNAME' => $vals[$index['HOMETEAMNAME'][$i]]['value'],
				'AWAYTEAMID' => $vals[$index['AWAYTEAMID'][$i]]['value'],
				'AWAYTEAMNAME' => $vals[$index['AWAYTEAMNAME'][$i]]['value'],
				'MATCHDATE' => $vals[$index['MATCHDATE'][$i]]['value'],
				'MATCHTYPE' => $vals[$index['MATCHTYPE'][$i]]['value'],
				'HOMEGOALS' => $vals[$index['HOMEGOALS'][$i]]['value'],
				'AWAYGOALS' => $vals[$index['AWAYGOALS'][$i]]['value'],
				'STATUS' => $vals[$index['STATUS'][$i]]['value']
			);
		}
		$this->matches = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'MATCH' => $match
		);
		return $this->matches;
	}
	else return false;
}

/**
 * Remove cached information about matches archive.
 *
 * @access   public
 */
function clearMatchesArchive()
{
	$this->matchesArchive = false;
}

/**
 * Retrieve matches archive for team $id, or for user's own team.
 *
 * @param    string   teamID
 * @access   public
 */
function getMatchesArchiveXML($id = false, $cup = false, $first = false, $last = false, $season = false) {

	$form = array('outputType' => 'XML');
	if ($cup)
		$form['actionType'] = 'viewCup';
	else
		$form['actionType'] = 'view';
	if ($id)
		$form['teamID'] = $id;
	if ($first)
		$form['FirstMatchDate'] = $first;
	if ($last)
		$form['LastMatchDate'] = $last;
	if ($season)
		$form['Season'] = $season;
	$code = $this->http->post(HT_MATCHESARCHIVE, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve matches archive for team $id, or for user's own team.
 *
 * @param    string   teamID
 * @access   public
 */
function getMatchesArchive($id = false, $cup = false, $first = false, $last = false, $season = false) {
	if ((! $id) && ($this->matchesArchive))
		return $this->matchesArchive;
	$xml = $this->getMatchesArchiveXML($id, $cup, $first, $last, $season);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['MATCHID']); $i++) {
			$match[$i] = array(
				'MATCHID' => $vals[$index['MATCHID'][$i]]['value'],
				'HOMETEAMID' => $vals[$index['HOMETEAMID'][$i]]['value'],
				'HOMETEAMNAME' => $vals[$index['HOMETEAMNAME'][$i]]['value'],
				'AWAYTEAMID' => $vals[$index['AWAYTEAMID'][$i]]['value'],
				'AWAYTEAMNAME' => $vals[$index['AWAYTEAMNAME'][$i]]['value'],
				'MATCHDATE' => $vals[$index['MATCHDATE'][$i]]['value'],
				'MATCHTYPE' => $vals[$index['MATCHTYPE'][$i]]['value'],
				'HOMEGOALS' => $vals[$index['HOMEGOALS'][$i]]['value'],
				'AWAYGOALS' => $vals[$index['AWAYGOALS'][$i]]['value']
			);
		}
		$this->matchesArchive = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'FIRSTMATCHDATE' => $vals[$index['FIRSTMATCHDATE'][0]]['value'],
			'LASTMATCHDATE' => $vals[$index['LASTMATCHDATE'][0]]['value'],
			'MATCH' => $match
		);
		return $this->matchesArchive;
	}
	else return false;
}

/**
 * Remove cached information about league fixtures.
 *
 * @access   public
 */
function clearLeagueFixtures()
{
	$this->leagueFixtures = false;
}

/**
 * Retrieve league fixtures.
 *
 * @param    integer   leagueLevelUnitID
 * @param    integer   season
 * @access   public
 */
function getLeagueFixturesXML($id = false, $season = false)
{

	$form = array('outputType' => 'XML',
			'actionType' => 'view');
	if ($id)
		$form['leagueLevelUnitID'] = $id;
	if ($season)
		$form['season'] = $season;
	$code = $this->http->post(HT_FIXTURES, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve league fixtures.
 *
 * @param    integer   leagueLevelUnitID
 * @param    integer   season
 * @access   public
 */
function getLeagueFixtures($id = false, $season = false)
{
	if ((! $id) && ($this->fixtures))
		return $this->fixtures;
	$xml = $this->getLeagueFixturesXML($id, $season);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['MATCHID']); $i++) {
			$match[$i] = array(
				'MATCHID' => $vals[$index['MATCHID'][$i]]['value'],
				'MATCHROUND' => $vals[$index['MATCHROUND'][$i]]['value'],
				'HOMETEAMID' => $vals[$index['HOMETEAMID'][$i]]['value'],
				'HOMETEAMNAME' => $vals[$index['HOMETEAMNAME'][$i]]['value'],
				'AWAYTEAMID' => $vals[$index['AWAYTEAMID'][$i]]['value'],
				'AWAYTEAMNAME' => $vals[$index['AWAYTEAMNAME'][$i]]['value'],
				'MATCHDATE' => $vals[$index['MATCHDATE'][$i]]['value'],
				'HOMEGOALS' => $vals[$index['HOMEGOALS'][$i]]['value'],
				'AWAYGOALS' => $vals[$index['AWAYGOALS'][$i]]['value']
			);
		}
		$this->fixtures = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'LEAGUELEVELUNITID' => $vals[$index['LEAGUELEVELUNITID'][0]]['value'],
			'LEAGUELEVELUNITNAME' => $vals[$index['LEAGUELEVELUNITNAME'][0]]['value'],
			'SEASON' => $vals[$index['SEASON'][0]]['value'],
			'MATCH' => $match
		);
		return $this->fixtures;
	}
	else return false;
}

/**
 * Retrieve specified match.
 *
 * @access   public
 * @param    string   MatchID
 */
function getMatchXML($id)
{
	$form = array('outputType' => 'XML',
		'actionType' => 'view',
		'matchID' => $id
	);
	$code = $this->http->post(HT_MATCH, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve specified match.
 *
 * @access   public
 * @param    string   MatchID
 */
function getMatch($id = false)
{
	$xml = $this->getMatchXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['SCORERPLAYERID']); $i++) {
			$goal[$i] = array(
				'SCORERPLAYERID' => $vals[$index['SCORERPLAYERID'][$i]]['value'],
				'SCORERPLAYERNAME' => $vals[$index['SCORERPLAYERNAME'][$i]]['value'],
				'SCORERTEAMID' => $vals[$index['SCORERTEAMID'][$i]]['value'],
				'SCORERHOMEGOALS' => $vals[$index['SCORERHOMEGOALS'][$i]]['value'],
				'SCORERAWAYGOALS' => $vals[$index['SCORERAWAYGOALS'][$i]]['value'],
				'SCORERMINUTE' => $vals[$index['SCORERMINUTE'][$i]]['value']
			);
		}
		for ($i = 0; $i < count($index['BOOKINGPLAYERID']); $i++) {
			$booking[$i] = array(
				'BOOKINGPLAYERID' => $vals[$index['BOOKINGPLAYERID'][$i]]['value'],
				'BOOKINGPLAYERNAME' => $vals[$index['BOOKINGPLAYERNAME'][$i]]['value'],
				'BOOKINGTEAMID' => $vals[$index['BOOKINGTEAMID'][$i]]['value'],
				'BOOKINGTYPE' => $vals[$index['BOOKINGTYPE'][$i]]['value'],
				'BOOKINGMINUTE' => $vals[$index['BOOKINGMINUTE'][$i]]['value']
			);
		}
		$match = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'MATCHID' => $vals[$index['MATCHID'][0]]['value'],
			'MATCHDATE' => $vals[$index['MATCHDATE'][0]]['value'],
			'FINISHEDDATE' => $vals[$index['FINISHEDDATE'][0]]['value'],
			'HOMETEAMID' => $vals[$index['HOMETEAMID'][0]]['value'],
			'HOMETEAMNAME' => $vals[$index['HOMETEAMNAME'][0]]['value'],
			'HOMEGOALS' => $vals[$index['HOMEGOALS'][0]]['value'],
			'HOMETEAM' => array(
				'DRESS' => $vals[$index['DRESS'][0]]['value'],
				'TACTICTYPE' => $vals[$index['TACTICTYPE'][0]]['value'],
				'TACTICSKILL' => $vals[$index['TACTICSKILL'][0]]['value'],
				'RATINGMIDFIELD' => $vals[$index['RATINGMIDFIELD'][0]]['value'],
				'RATINGRIGHTDEF' => $vals[$index['RATINGRIGHTDEF'][0]]['value'],
				'RATINGMIDDEF' => $vals[$index['RATINGMIDDEF'][0]]['value'],
				'RATINGLEFTDEF' => $vals[$index['RATINGLEFTDEF'][0]]['value'],
				'RATINGRIGHTATT' => $vals[$index['RATINGRIGHTATT'][0]]['value'],
				'RATINGMIDATT' => $vals[$index['RATINGMIDATT'][0]]['value'],
				'RATINGLEFTATT' => $vals[$index['RATINGLEFTATT'][0]]['value'],
				'TEAMATTITUDE' => $vals[$index['TEAMATTITUDE'][0]]['value']
			),
			'AWAYTEAMID' => $vals[$index['AWAYTEAMID'][0]]['value'],
			'AWAYTEAMNAME' => $vals[$index['AWAYTEAMNAME'][0]]['value'],
			'AWAYGOALS' => $vals[$index['AWAYGOALS'][0]]['value'],
			'AWAYTEAM' => array(
				'DRESS' => $vals[$index['DRESS'][1]]['value'],
				'TACTICTYPE' => $vals[$index['TACTICTYPE'][1]]['value'],
				'TACTICSKILL' => $vals[$index['TACTICSKILL'][1]]['value'],
				'RATINGMIDFIELD' => $vals[$index['RATINGMIDFIELD'][1]]['value'],
				'RATINGRIGHTDEF' => $vals[$index['RATINGRIGHTDEF'][1]]['value'],
				'RATINGMIDDEF' => $vals[$index['RATINGMIDDEF'][1]]['value'],
				'RATINGLEFTDEF' => $vals[$index['RATINGLEFTDEF'][1]]['value'],
				'RATINGRIGHTATT' => $vals[$index['RATINGRIGHTATT'][1]]['value'],
				'RATINGMIDATT' => $vals[$index['RATINGMIDATT'][1]]['value'],
				'RATINGLEFTATT' => $vals[$index['RATINGLEFTATT'][1]]['value'],
				'TEAMATTITUDE' => $vals[$index['TEAMATTITUDE'][1]]['value']
			),
			'ARENAID' => $vals[$index['ARENAID'][0]]['value'],
			'ARENANAME' => $vals[$index['ARENANAME'][0]]['value'],
			'WEATHERID' => $vals[$index['WEATHERID'][0]]['value'],
			'SOLDTOTAL' => $vals[$index['SOLDTOTAL'][0]]['value'],
			'SOLDTERRACES' => $vals[$index['SOLDTERRACES'][0]]['value'],
			'SOLDBASIC' => $vals[$index['SOLDBASIC'][0]]['value'],
			'SOLDROOF' => $vals[$index['SOLDROOF'][0]]['value'],
			'SOLDVIP' => $vals[$index['SOLDVIP'][0]]['value'],
			'GOAL' => $goal,
			'BOOKING' => $booking
		);
		return $match;
	}
	else return false;
}

/**
 * Retrieve specified match lineup.
 *
 * @access   public
 * @param    string   MatchID
 * @param    string   TeamID
 */
function getMatchLineupXML($matchid = false, $teamid = false)
{
	$form = array('outputType' => 'XML',
		'actionType' => 'view'
	);
	if ($matchid)
		$form['matchID'] = $matchid;
	if ($teamid)
		$form['teamID'] = $teamid;
	$code = $this->http->post(HT_MATCHLINEUP, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve specified match lineup.
 *
 * @access   public
 * @param    string   MatchID
 * @param    string   TeamID
 */
function getMatchLineup($matchid = false, $teamid = false)
{
	$xml = $this->getMatchLineupXML($matchid, $teamid);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['PLAYERID']); $i++) {
			$player[$i] = array(
				'PLAYERID' => $vals[$index['PLAYERID'][$i]]['value'],
				'ROLEID' => $vals[$index['ROLEID'][$i]]['value'],
				'PLAYERNAME' => $vals[$index['PLAYERNAME'][$i]]['value'],
				'RATINGSTARS' => $vals[$index['RATINGSTARS'][$i]]['value'],
				'POSITIONCODE' => $vals[$index['POSITIONCODE'][$i]]['value'],
				'BEHAVIOUR' => $vals[$index['BEHAVIOUR'][$i]]['value']
			);
		}
		$match = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'MATCHID' => $vals[$index['MATCHID'][0]]['value'],
			'HOMETEAMID' => $vals[$index['HOMETEAMID'][0]]['value'],
			'HOMETEAMNAME' => $vals[$index['HOMETEAMNAME'][0]]['value'],
			'AWAYTEAMID' => $vals[$index['AWAYTEAMID'][0]]['value'],
			'AWAYTEAMNAME' => $vals[$index['AWAYTEAMNAME'][0]]['value'],
			'MATCHTYPE' => $vals[$index['MATCHTYPE'][0]]['value'],
			'ARENAID' => $vals[$index['ARENAID'][0]]['value'],
			'ARENANAME' => $vals[$index['ARENANAME'][0]]['value'],
			'MATCHDATE' => $vals[$index['MATCHDATE'][0]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'EXPERIENCELEVEL' => $vals[$index['EXPERIENCELEVEL'][0]]['value'],
			'PLAYER' => $player
		);
		return $match;
	}
	else return false;
}

/**
 * Retrieve specified match description, suitable to for finished matches.
 *
 * @access   public
 * @param    string   MatchID
 */
function getLiveMatchXML($id)
{
	$form = array('outputType' => 'XML',
		'actionType' => 'deleteMatch',
		'matchID' => $id
	);
	$code = $this->http->post(HT_LIVE, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	$form = array('outputType' => 'XML',
		'actionType' => 'addMatch',
		'matchID' => $id
	);
	$code = $this->http->post(HT_LIVE, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	$live = $this->http->get_response_body();

	$form = array('outputType' => 'XML',
		'actionType' => 'deleteMatch',
		'matchID' => $id
	);
	$code = $this->http->post(HT_LIVE, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $live;
}

/**
 * Retrieve specified match description, suitable to for finished matches.
 *
 * @access   public
 * @param    string   MatchID
 */
function getLiveMatch($id)
{
	$xml = $this->getLiveMatchXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['EVENTKEY']); $i++) {
			$event[$i] = array(
				'SUBJECTPLAYERID' => $vals[$index['SUBJECTPLAYERID'][$i]]['value'],
				'SUBJECTTEAMID' => $vals[$index['SUBJECTTEAMID'][$i]]['value'],
				'OBJECTPLAYERID' => $vals[$index['OBJECTPLAYERID'][$i]]['value'],
				'MINUTE' => $vals[$index['MINUTE'][$i]]['value'],
				'EVENTKEY' => $vals[$index['EVENTKEY'][$i]]['value'],
				'EVENTTEXT' => $vals[$index['EVENTTEXT'][$i]]['value']
			);
		}
		$match = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'MATCHID' => $vals[$index['MATCHID'][0]]['value'],
			'MATCHDATE' => $vals[$index['MATCHDATE'][0]]['value'],
			'HOMETEAMID' => $vals[$index['HOMETEAMID'][0]]['value'],
			'HOMETEAMNAME' => $vals[$index['HOMETEAMNAME'][0]]['value'],
			'AWAYTEAMID' => $vals[$index['AWAYTEAMID'][0]]['value'],
			'AWAYTEAMNAME' => $vals[$index['AWAYTEAMNAME'][0]]['value'],
			'EVENT' => $event
		);
		return $match;
	}
	else return false;
}

/**
 * Remove cached information about the logged in users' team training,
 * formation experience, team spirit and confidence.
 *
 * @access   public
 */
function clearTraining()
{
	$this->training = false;
}

/**
 * Retrieve XML with data about the logged in users' team training,
 * formation experience, team spirit and confidence.
 *
 * @access   public
 */
function getTrainingXML()
{
	$form = array('outputType' => 'XML',
		'actionType' => 'view'
	);
	$code = $this->http->post(HT_TRAINING, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve data about the logged in users' team training, formation
 * experience, team spirit and confidence.
 *
 * @access   public
 */
function getTraining()
{
	if ($this->training)
		return $this->training;
	$xml = $this->getTrainingXML();
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		$this->training = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'TRAININGLEVEL' => $vals[$index['TRAININGLEVEL'][0]]['value'],
			'NEWTRAININGLEVEL' => $vals[$index['NEWTRAININGLEVEL'][0]]['value'],
			'TRAININGTYPE' => $vals[$index['TRAININGTYPE'][0]]['value'],
			'TRAINERID' => $vals[$index['TRAINERID'][0]]['value'],
			'TRAINERNAME' => $vals[$index['TRAINERNAME'][0]]['value'],
			'ARRIVALDATE' => $vals[$index['ARRIVALDATE'][0]]['value'],
			'MORALE' => $vals[$index['MORALE'][0]]['value'],
			'SELFCONFIDENCE' => $vals[$index['SELFCONFIDENCE'][0]]['value'],
			'EXPERIENCE433' => $vals[$index['EXPERIENCE433'][0]]['value'],
			'EXPERIENCE451' => $vals[$index['EXPERIENCE451'][0]]['value'],
			'EXPERIENCE352' => $vals[$index['EXPERIENCE352'][0]]['value'],
			'EXPERIENCE532' => $vals[$index['EXPERIENCE532'][0]]['value'],
			'EXPERIENCE343' => $vals[$index['EXPERIENCE343'][0]]['value'],
			'EXPERIENCE541' => $vals[$index['EXPERIENCE541'][0]]['value']
		);
		return $this->training;
	}
	else return false;
}

/**
 * Remove cached information about training stats.
 *
 * @access   public
 */
function clearTrainingStats()
{
	$this->trainingStats = false;
}

/**
 * Retrieve global training stats XML, or training stats XML for league $id.
 *
 * @param    string   LeagueID
 * @access   public
 */
function getTrainingStatsXML($id = false)
{
	if ($id)
		$form = array('outputType' => 'XML',
			'actionType' => 'stats',
			'StatsLeagueID' => $id
		);
	else
		$form = array('outputType' => 'XML',
			'actionType' => 'stats'
		);
	$code = $this->http->post(HT_TRAINING, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve global training stats, or training stats for league $id.
 *
 * @param    string   LeagueID
 * @access   public
 */
function getTrainingStats($id = false)
{
	if ($this->trainingStats)
		return $this->trainingStats;
	$xml = $this->getTrainingStatsXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['TRAININGTYPE']); $i++) {
			$trainingStat[$i] = array(
				'TRAININGTYPE' => $vals[$index['TRAININGTYPE'][$i]]['value'],
				'NUMBEROFTEAMS' => $vals[$index['NUMBEROFTEAMS'][$i]]['value'],
				'FRACTIONOFTEAMS' => $vals[$index['FRACTIONOFTEAMS'][$i]]['value']
			);
		}
		$this->trainingStats = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'TRAININGSTAT' => $trainingStat
		);
		return $this->trainingStats;
	}
	else return false;
}

/**
 * Remove cached player list.
 *
 * @access   public
 */
function clearPlayers()
{
	$this->players = false;
}

/**
 * Retrieve players list.
 *
 * @param    string   TeamID
 * @access   public
 */
function getPlayersXML($id = false)
{
	if ($id)
		$form = array('outputType' => 'XML',
			'actionType' => 'view',
			'TeamID' => $id
		);
	else
		$form = array('outputType' => 'XML',
			'actionType' => 'view'
		);
	$code = $this->http->post(HT_PLAYERS, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve players list.
 *
 * @param    string   TeamID
 * @access   public
 */
function getPlayers($id = false)
{
	if ((! $id) && ($this->players))
		return $this->players;
	$xml = $this->getPlayersXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		for ($i = 0; $i < count($index['PLAYERID']); $i++) {
			$player[$i] = array(
				'PLAYERID' => $vals[$index['PLAYERID'][$i]]['value'],
				'PLAYERNAME' => $vals[$index['PLAYERNAME'][$i]]['value'],
				'PLAYERNUMBER' => $vals[$index['PLAYERNUMBER'][$i]]['value'],
				'AGE' => $vals[$index['AGE'][$i]]['value'],
				'MARKETVALUE' => $vals[$index['MARKETVALUE'][$i]]['value'],
				'TSI' => $vals[$index['TSI'][$i]]['value'],
				'PLAYERFORM' => $vals[$index['PLAYERFORM'][$i]]['value'],
				'STATEMENT' => $vals[$index['STATEMENT'][$i]]['value'],
				'EXPERIENCE' => $vals[$index['EXPERIENCE'][$i]]['value'],
				'LEADERSHIP' => $vals[$index['LEADERSHIP'][$i]]['value'],
				'SALARY' => $vals[$index['SALARY'][$i]]['value'],
				'ISABROAD' => $vals[$index['ISABROAD'][$i]]['value'],
				'AGREEABILITY' => $vals[$index['AGREEABILITY'][$i]]['value'],
				'AGGRESSIVENESS' => $vals[$index['AGGRESSIVENESS'][$i]]['value'],
				'HONESTY' => $vals[$index['HONESTY'][$i]]['value'],
				'LEAGUEGOALS' => $vals[$index['LEAGUEGOALS'][$i]]['value'],
				'CUPGOALS' => $vals[$index['CUPGOALS'][$i]]['value'],
				'FRIENDLIESGOALS' => $vals[$index['FRIENDLIESGOALS'][$i]]['value'],
				'CAREERGOALS' => $vals[$index['CAREERGOALS'][$i]]['value'],
				'CAREERHATTRICKS' => $vals[$index['CAREERHATTRICKS'][$i]]['value'],
				'SPECIALTY' => $vals[$index['SPECIALTY'][$i]]['value'],
				'TRANSFERLISTED' => $vals[$index['TRANSFERLISTED'][$i]]['value'],
				'NATIONALTEAMID' => $vals[$index['NATIONALTEAMID'][$i]]['value'],
				'COUNTRYID' => $vals[$index['COUNTRYID'][$i]]['value'],
				'CAPS' => $vals[$index['CAPS'][$i]]['value'],
				'CAPSU20' => $vals[$index['CAPSU20'][$i]]['value'],
				'CARDS' => $vals[$index['CARDS'][$i]]['value'],
				'INJURYLEVEL' => $vals[$index['INJURYLEVEL'][$i]]['value'],
				'STAMINASKILL' => $vals[$index['STAMINASKILL'][$i]]['value'],
				'KEEPERSKILL' => $vals[$index['KEEPERSKILL'][$i]]['value'],
				'PLAYMAKERSKILL' => $vals[$index['PLAYMAKERSKILL'][$i]]['value'],
				'SCORERSKILL' => $vals[$index['SCORERSKILL'][$i]]['value'],
				'PASSINGSKILL' => $vals[$index['PASSINGSKILL'][$i]]['value'],
				'WINGERSKILL' => $vals[$index['WINGERSKILL'][$i]]['value'],
				'DEFENDERSKILL' => $vals[$index['DEFENDERSKILL'][$i]]['value'],
				'SETPIECESSKILL' => $vals[$index['SETPIECESSKILL'][$i]]['value'],
				'TRAINERTYPE' => $vals[$index['TRAINERTYPE'][0]]['value'],
				'TRAINERSKILL' => $vals[$index['TRAINERSKILL'][0]]['value']
			);
		}
		$this->players = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'PLAYER' => $player
		);
		return $this->players;
	}
	else return false;
}

/**
 * Retrieve details about player identified by passed PlayerID.
 *
 * @param    string   PlayerID
 * @access   public
 */
function getPlayerXML($id)
{
	$form = array('outputType' => 'XML',
		'actionType' => 'view',
		'playerID' => $id
	);
	$code = $this->http->post(HT_PLAYER, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve details about player identified by passed PlayerID.
 *
 * @param    string   PlayerID
 * @access   public
 */
function getPlayer($id)
{
	$xml = $this->getPlayerXML($id);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		$player = array(
			'FILENAME' => $vals[$index['FILENAME'][0]]['value'],
			'VERSION' => $vals[$index['VERSION'][0]]['value'],
			'USERID' => $vals[$index['USERID'][0]]['value'],
			'USERISSUPPORTER' => $vals[$index['USERISSUPPORTER'][0]]['value'],
			'USERHASCLUBHOUS' => $vals[$index['USERHASCLUBHOUS'][0]]['value'],
			'FETCHEDDATE' => $vals[$index['FETCHEDDATE'][0]]['value'],
			'PLAYERID' => $vals[$index['PLAYERID'][$i]]['value'],
			'PLAYERNAME' => $vals[$index['PLAYERNAME'][$i]]['value'],
			'PLAYERNUMBER' => $vals[$index['PLAYERNUMBER'][$i]]['value'],
			'AGE' => $vals[$index['AGE'][$i]]['value'],
			'PLAYERFORM' => $vals[$index['PLAYERFORM'][$i]]['value'],
			'CARDS' => $vals[$index['CARDS'][$i]]['value'],
			'INJURYLEVEL' => $vals[$index['INJURYLEVEL'][$i]]['value'],
			'STATEMENT' => $vals[$index['STATEMENT'][$i]]['value'],
			'PLAYERLANGUAGE' => $vals[$index['PLAYERLANGUAGE'][$i]]['value'],
			'TRAINERTYPE' => $vals[$index['TRAINERTYPE'][0]]['value'],
			'TRAINERSKILL' => $vals[$index['TRAINERSKILL'][0]]['value'],
			'AGREEABILITY' => $vals[$index['AGREEABILITY'][$i]]['value'],
			'AGGRESSIVENESS' => $vals[$index['AGGRESSIVENESS'][$i]]['value'],
			'HONESTY' => $vals[$index['HONESTY'][$i]]['value'],
			'EXPERIENCE' => $vals[$index['EXPERIENCE'][$i]]['value'],
			'LEADERSHIP' => $vals[$index['LEADERSHIP'][$i]]['value'],
			'SPECIALTY' => $vals[$index['SPECIALTY'][$i]]['value'],
			'NATIVELEAGUEID' => $vals[$index['NATIVELEAGUEID'][$i]]['value'],
			'NATIVELEAGUENAME' => $vals[$index['NATIVELEAGUENAME'][$i]]['value'],
			'TSI' => $vals[$index['TSI'][$i]]['value'],
			'TEAMID' => $vals[$index['TEAMID'][0]]['value'],
			'TEAMNAME' => $vals[$index['TEAMNAME'][0]]['value'],
			'LEAGUEID' => $vals[$index['LEAGUEID'][$i]]['value'],
			'SALARY' => $vals[$index['SALARY'][$i]]['value'],
			'ISABROAD' => $vals[$index['ISABROAD'][$i]]['value'],
			'STAMINASKILL' => $vals[$index['STAMINASKILL'][$i]]['value'],
			'KEEPERSKILL' => $vals[$index['KEEPERSKILL'][$i]]['value'],
			'PLAYMAKERSKILL' => $vals[$index['PLAYMAKERSKILL'][$i]]['value'],
			'SCORERSKILL' => $vals[$index['SCORERSKILL'][$i]]['value'],
			'PASSINGSKILL' => $vals[$index['PASSINGSKILL'][$i]]['value'],
			'WINGERSKILL' => $vals[$index['WINGERSKILL'][$i]]['value'],
			'DEFENDERSKILL' => $vals[$index['DEFENDERSKILL'][$i]]['value'],
			'SETPIECESSKILL' => $vals[$index['SETPIECESSKILL'][$i]]['value'],
			'CAPS' => $vals[$index['CAPS'][$i]]['value'],
			'CAPSU20' => $vals[$index['CAPSU20'][$i]]['value'],
			'CAREERGOALS' => $vals[$index['CAREERGOALS'][$i]]['value'],
			'CAREERHATTRICKS' => $vals[$index['CAREERHATTRICKS'][$i]]['value'],
			'LEAGUEGOALS' => $vals[$index['LEAGUEGOALS'][$i]]['value'],
			'CUPGOALS' => $vals[$index['CUPGOALS'][$i]]['value'],
			'NATIONALTEAMID' => $vals[$index['NATIONALTEAMID'][$i]]['value'],
			'NATIONALTEAMNAME' => $vals[$index['NATIONALTEAMNAME'][$i]]['value'],
			'TRANSFERLISTED' => $vals[$index['TRANSFERLISTED'][$i]]['value']
		);
		return $player;
	}
	else return false;
}

/**
 * Remove cached information about transfer history.
 *
 * @access   public
 */
function clearTransferHistory()
{
	$this->transfer = false;
}

/**
 * Retrieve transfer history details for team or player identified by ID $id.
 *
 * @param    string   actionType
 * @param    string   Team or Player ID
 * @access   public
 */
function getTransferHistoryXML($id, $type = 'player')
{
	if ($type == 'team')
		$form = array('outputType' => 'XML',
			'actionType' => 'team',
			'teamID' => $id
		);
	else
		$form = array('outputType' => 'XML',
			'actionType' => 'player',
			'playerID' => $id
		);
	$code = $this->http->post(HT_TRANSFERS, $form, 'http://'.$this->http->host.HT_HOME);
	if ($code != 200)
		return false;
	return $this->http->get_response_body();
}

/**
 * Retrieve transfer history details for team or player identified by ID $id.
 *
 * @param    string   actionType
 * @param    string   Team or Player ID
 * @access   public
 */
function getTransferHistory($id, $type = 'player')
{
	$xml = $this->getTransferHistoryXML($id, $type);
	if ($xml) {
		$parser = xml_parser_create();
		xml_parse_into_struct($parser, $xml, $vals, $index);
		xml_parser_free($parser);
		if (! $vals[$index['FILENAME'][0]]['value'])
			return false;
		if ($type == 'team') {
			for ($i = 0; $i < count($index['TRANSFERID']); $i++) {
				$transfer[$i] = array(
					'TRANSFERID' => $vals[$index['TRANSFERID'][$i]]['value'],
					'DEADLINE' => $vals[$index['DEADLINE'][$i]]['value'],
					'TRANSFERTYPE' => $vals[$index['TRANSFERTYPE'][$i]]['value'],
					'PLAYERID' => $vals[$index['PLAYERID'][0]]['value'],
					'PLAYERNAME' => $vals[$index['PLAYERNAME'][0]]['value'],
					'BUYERTEAMID' => $vals[$index['BUYERTEAMID'][$i]]['value'],
					'BUYERTEAMNAME' => $vals[$index['BUYERTEAMNAME'][$i]]['value'],
					'SELLERTEAMID' => $vals[$index['SELLERTEAMID'][$i]]['value'],
					'SELLERTEAMNAME' => $vals[$index['SELLERTEAMNAME'][$i]]['value'],
					'PRICE' => $vals[$index['PRICE'][$i]]['value'],
					'MARKETVALUE' => $vals[$index['MARKETVALUE'][$i]]['value'],
					'TSI' => $vals[$index['TSI'][$i]]['value']
				);
			}
			$this->transfer = array(
				'FILENAME'		=> $vals[$index['FILENAME'][0]]['value'],
				'VERSION'		=> $vals[$index['VERSION'][0]]['value'],
				'USERID'		=> $vals[$index['USERID'][0]]['value'],
				'FETCHEDDATE'		=> $vals[$index['FETCHEDDATE'][0]]['value'],
				'TEAMID'		=> $vals[$index['TEAMID'][0]]['value'],
				'TEAMNAME'		=> $vals[$index['TEAMNAME'][0]]['value'],
				'ACTIVATEDDATE'		=> $vals[$index['ACTIVATEDDATE'][0]]['value'],
				'TOTALSUMOFBUYS'	=> $vals[$index['TOTALSUMOFBUYS'][0]]['value'],
				'TOTALSUMOFSALES'	=> $vals[$index['TOTALSUMOFSALES'][0]]['value'],
				'NUMBEROFBUYS'		=> $vals[$index['NUMBEROFBUYS'][0]]['value'],
				'NUMBEROFSALES'		=> $vals[$index['NUMBEROFSALES'][0]]['value'],
				'STARTDATE'		=> $vals[$index['STARTDATE'][0]]['value'],
				'ENDDATE'		=> $vals[$index['ENDDATE'][0]]['value'],
				'TRANSFER'	=> $transfer
			);
		}
		else {
			for ($i = 0; $i < count($index['TRANSFERID']); $i++) {
				$transfer[$i] = array(
					'TRANSFERID' => $vals[$index['TRANSFERID'][$i]]['value'],
					'DEADLINE' => $vals[$index['DEADLINE'][$i]]['value'],
					'BUYERTEAMID' => $vals[$index['BUYERTEAMID'][$i]]['value'],
					'BUYERTEAMNAME' => $vals[$index['BUYERTEAMNAME'][$i]]['value'],
					'SELLERTEAMID' => $vals[$index['SELLERTEAMID'][$i]]['value'],
					'SELLERTEAMNAME' => $vals[$index['SELLERTEAMNAME'][$i]]['value'],
					'PRICE' => $vals[$index['PRICE'][$i]]['value'],
					'MARKETVALUE' => $vals[$index['MARKETVALUE'][$i]]['value'],
					'TSI' => $vals[$index['TSI'][$i]]['value']
				);
			}
			$this->transfer = array(
				'FILENAME'	=> $vals[$index['FILENAME'][0]]['value'],
				'VERSION'	=> $vals[$index['VERSION'][0]]['value'],
				'USERID'	=> $vals[$index['USERID'][0]]['value'],
				'FETCHEDDATE'	=> $vals[$index['FETCHEDDATE'][0]]['value'],
				'STARTDATE'	=> $vals[$index['STARTDATE'][0]]['value'],
				'ENDDATE'	=> $vals[$index['ENDDATE'][0]]['value'],
				'PLAYERID'	=> $vals[$index['PLAYERID'][0]]['value'],
				'PLAYERNAME'	=> $vals[$index['PLAYERNAME'][0]]['value'],
				'TRANSFER'	=> $transfer
			);
			return $this->transfer;
		}
	}
	else return false;
}


/* ==> Group 2: Utility functions */

/**
 * Get first and last name of players, filling two new array items.
 *
 * @access   public
 * @param    array   Players
 * @param    integer Max name lenght
 */
function splitPlayerNames($players, $max = false)
{
	for ($i = 0; $i < count($players['PLAYER']); $i++) {
		$fullname = $players['PLAYER'][$i]['PLAYERNAME'];
		if (strpos($fullname, '  ') == false) {
			// it is an old player
			if (strpos($fullname, ' ') == false)
				// old and/or with a very strange name...
				$fullname = $fullname.'  '.$fullname;
			else
				$fullname = str_replace(' ', '  ', $fullname);
		}
		$players['PLAYER'][$i]['PlayerFirstName'] = substr($fullname, 0, strpos($fullname, '  '));
		$players['PLAYER'][$i]['PlayerLastName'] = substr($fullname, strpos($fullname, '  ')+2);
		if ($max) {
			if (strlen($players['PLAYER'][$i]['PlayerFirstName']) > $max)
				$players['PLAYER'][$i]['PlayerFirstName'] = substr($players['PLAYER'][$i]['PlayerFirstName'], 0, $max-1).'.';
			if (strlen($players['PLAYER'][$i]['PlayerLastName']) > $max)
				$players['PLAYER'][$i]['PlayerLastName'] = substr($players['PLAYER'][$i]['PlayerLastName'], 0, $max-1).'.';
		}
/*
		$players['PLAYER'][$i]['PlayerFirstName'] = htmlentities($players['PLAYER'][$i]['PlayerFirstName']);
		$players['PLAYER'][$i]['PlayerLastName'] = htmlentities($players['PLAYER'][$i]['PlayerLastName']);
*/
	}
	return($players);
}

/**
 * Returns training amount for specified PositionCode and TrainingType.
 *
 * @access   public
 * @param    integer PositionCode
 * @param    integer TrainingType
 */
function isTrained($code, $type)
{
	$amount = 0;
	switch ($type) {
		case T_STAMINA:
			$amount = 1;
			break;
		case T_SETPIECES:
			$amount = 1;
			break;
		case T_DEFENDING:
			if (($code > 1) && ($code < 6)) $amount = 1;
			break;
		case T_SCORING:
			if (($code > 9) && ($code < 12)) $amount = 1;
			break;
		case T_WINGER:
			if (($code == 2) || ($code == 5)) $amount = 0.5;
			else if (($code == 6) || ($code == 9)) $amount = 1;
			break;
		case T_SHOOTING:
			if (($code > 1) && ($code < 12)) $amount = 0.5;
			break;
		case T_SHORTPASSES:
			if (($code > 5) && ($code < 12)) $amount = 1;
			break;
		case T_PLAYMAKER:
			if (($code == 6) || ($code == 9)) $amount = 0.5;
			else if (($code == 7) || ($code == 8)) $amount = 1;
			break;
		case T_GOALTENDING:
			if ($code == 1) $amount = 1;
			break;
		case T_THROUGHPASSES:
			if (($code > 1) && ($code < 10)) $amount = 1;
			break;
		case T_DEFENSIVEPOSITIONS:
			if (($code > 1) && ($code < 10)) $amount = 0.5;
			break;
		case T_WINGATTACKS:
			if (($code == 6) || (($code > 8) && ($code < 12))) $amount = 1;
			break;
/*
		case T_GENERAL: // General
			break;
*/
		default:
			break;
	}
	return $amount;
}

/**
 * Get amount of training needed for a jump in each skill.
 *
 * @access   public
 * @param    integer Age of player
 * @param    integer Training level (%)
 * @param    integer Trainer skill
 * @param    integer Keeper trainers
 * @param    integer Assistant trainers
 */
function getTrainingWeeks($age, $level, $tr = 7, $kt = 10, $at = 10)
{
	$l_diff = (7 - $tr);
	$l_diff += $age - 17;

	$weeks['Keeper'] = TLEN_KEEPER;
	if ($kt < 9) $l_keeper++;
	if ($kt < 5) $l_keeper++;
	if ($kt < 3) $l_keeper++;
	$weeks['Keeper'] += $l_diff;

	if ($at < 9) $l_diff++;
	if ($at < 5) $l_diff++;
	if ($at < 3) $l_diff++;
	$weeks['Playmaker'] = TLEN_PLAYMAKER+$l_diff;
	$weeks['Scorer'] = TLEN_SCORER+$l_diff;
	$weeks['Passing'] = TLEN_PASSING+$l_diff;
	$weeks['Winger'] = TLEN_WINGER+$l_diff;
	$weeks['Defender'] = TLEN_DEFENDER+$l_diff;
	return $weeks;
}

/**
 * Evaluate training influence for each skill.
 *
 * @access   public
 * @param    array   Player
 * @param    integer Training level (%)
 * @param    integer Trainer skill
 * @param    integer Keeper trainers
 * @param    integer Assistant trainers
 */
function evaluateTraining($player, $level, $tr = 7, $kt = 10, $at = 10)
{
	$weeks = $this->getTrainingWeeks($player['AGE'], $level, $tr, $kt, $at);
	if ($player['KeeperTrainings']) {
		$val = $player['KeeperTrainings']/$weeks['Keeper'];
		if ($val > 1) $val = 0.9;
		$player['KEEPERSKILL'] += $val;
	}
	if ($player['PlaymakerTrainings']) {
		$val = $player['PlaymakerTrainings']/$weeks['Playmaker'];
		if ($val > 1) $val = 0.9;
		$player['PLAYMAKERSKILL'] += $val;
	}
	if ($player['ScorerTrainings']) {
		$val = $player['ScorerTrainings']/$weeks['Scorer'];
		if ($val > 1) $val = 0.9;
		$player['SCORERSKILL'] += $val;
	}
	if ($player['PassingTrainings']) {
		$val = $player['PassingTrainings']/$weeks['Passing'];
		if ($val > 1) $val = 0.9;
		$player['PASSINGSKILL'] += $val;
	}
	if ($player['WingerTrainings']) {
		$val = $player['WingerTrainings']/$weeks['Winger'];
		if ($val > 1) $val = 0.9;
		$player['WINGERSKILL'] += $val;
	}
	if ($player['DefenderTrainings']) {
		$val = $player['DefenderTrainings']/$weeks['Defender'];
		if ($val > 1) $val = 0.9;
		$player['DEFENDERSKILL'] += $val;
	}
	return $player;
}

/**
 * Evaluate trainings influence for every player in each skill.
 *
 * @access   public
 * @param    array   Player
 * @param    integer Training level (%)
 * @param    integer Trainer skill
 * @param    integer Keeper trainers
 * @param    integer Assistant trainers
 */
function evaluateTrainings($players, $level, $tr = 7, $kt = 10, $at = 10)
{
	for ($i = 0; $i < count($players['PLAYER']); $i++)
		$players['PLAYER'][$i] = $this->evaluateTraining($players['PLAYER'][$i], $level, $tr, $kt, $at);
	return($players);
}

/**
 * Set training-meter for specified player and skill.
 *
 * @access   public
 * @param    array   Player
 * @param    integer Skill
 * @param    integer Value
 */
function setTraining($player, $skill, $value = 0)
{
	switch ($skill) {
		case T_STAMINA:
			if ($value > TLEN_STAMINA)
				$value = TLEN_STAMINA;
			$player['StaminaTrainings'] = $value;
			break;
		case T_SETPIECES:
			if ($value > TLEN_SETPIECES)
				$value = TLEN_SETPIECES;
			$player['SetPiecesTrainings'] = $value;
			break;
		case T_DEFENDING:
			if ($value > TLEN_DEFENDER)
				$value = TLEN_DEFENDER;
			$player['DefenderTrainings'] = $value;
			break;
		case T_SCORING:
			if ($value > TLEN_SCORER)
				$value = TLEN_SCORER;
			$player['ScorerTrainings'] = $value;
			break;
		case T_WINGER:
			if ($value > TLEN_WINGER)
				$value = TLEN_WINGER;
			$player['WingerTrainings'] = $value;
			break;
		case T_SHOOTING:
			if ($value > TLEN_SCORER)
				$value = TLEN_SCORER;
			$player['ScorerTrainings'] = $value;
			break;
		case T_SHORTPASSES:
			if ($value > TLEN_PASSING)
				$value = TLEN_PASSING;
			$player['PassingTrainings'] = $value;
			break;
		case T_PLAYMAKER:
			if ($value > TLEN_PLAYMAKER)
				$value = TLEN_PLAYMAKER;
			$player['PlaymakerTrainings'] = $value;
			break;
		case T_GOALTENDING:
			if ($value > TLEN_KEEPER)
				$value = TLEN_KEEPER;
			$player['KeeperTrainings'] = $value;
			break;
		case T_THROUGHPASSES:
			if ($value > TLEN_PASSING)
				$value = TLEN_PASSING;
			$player['PassingTrainings'] = $value;
			break;
		case T_DEFENSIVEPOSITIONS:
			if ($value > TLEN_DEFENDER)
				$value = TLEN_DEFENDER;
			$player['DefenderTrainings'] = $value;
			break;
		case T_WINGATTACKS:
			if ($value > TLEN_WINGER)
				$value = TLEN_WINGER;
			$player['WingerTrainings'] = $value;
			break;
		default:
			break;
	}
	return $player;
}

/**
 * Add some training amount for specified player and skill.
 *
 * @access   public
 * @param    array   Player
 * @param    integer Skill
 * @param    integer Value
 */
function addTraining($player, $skill, $value = 0)
{
	switch ($skill) {
		case T_STAMINA:
			$player['StaminaTrainings'] += $value;
			break;
		case T_SETPIECES:
			$player['SetPiecesTrainings'] += $value;
			break;
		case T_DEFENDING:
			$player['DefenderTrainings'] += $value;
			break;
		case T_SCORING:
			$player['ScorerTrainings'] += $value;
			break;
		case T_WINGER:
			$player['WingerTrainings'] += $value;
			break;
		case T_SHOOTING:
			$player['ScorerTrainings'] += $value;
			break;
		case T_SHORTPASSES:
			$player['PassingTrainings'] += $value;
			break;
		case T_PLAYMAKER:
			$player['PlaymakerTrainings'] += $value;
			break;
		case T_GOALTENDING:
			$player['KeeperTrainings'] += $value;
			break;
		case T_THROUGHPASSES:
			$player['PassingTrainings'] += $value;
			break;
		case T_DEFENSIVEPOSITIONS:
			$player['DefenderTrainings'] += $value;
			break;
		case T_WINGATTACKS:
			$player['WingerTrainings'] += $value;
			break;
		default:
			break;
	}
	return $player;
}

/**
 * Evaluate specified player in every role.
 *
 * @access   public
 * @param    array   Player
 */
function evaluatePlayer($player)
{
	$player['OASkill'] =
		((0.625 + $player['PLAYERFORM'] * 0.075) *
		(0.95 + $player['EXPERIENCE'] * 0.01));
	$player['GKSkill'] =
		($player['KEEPERSKILL']);
	$player['CDNSkill'] =
		($player['DEFENDERSKILL'] *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['CDOSkill'] =
		(0.5 * (0.35 + 0.13 * $player['STAMINASKILL']) *
		$player['PLAYMAKERSKILL'] +
		0.5 * $player['DEFENDERSKILL'] *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['CDESkill'] =
		((0.2 * $player['WINGERSKILL'] +
		0.8 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['WBDSkill'] =
		((0.01 * $player['WINGERSKILL'] +
		0.99 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['WBNSkill'] =
		((0.25 * $player['WINGERSKILL'] +
		0.75 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['WBOSkill'] =
		(0.03 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.45 * $player['WINGERSKILL'] +
		0.52 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['WBCSkill'] =
		((0.01 * $player['WINGERSKILL'] +
		0.99 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['MFDSkill'] =
		(0.7 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.05 * $player['PASSINGSKILL'] +
		0.25 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['MFNSkill'] =
		(0.7 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.15 * $player['PASSINGSKILL'] +
		0.15 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['MFOSkill'] =
		(0.7 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.25 * $player['PASSINGSKILL'] +
		0.05 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['MFESkill'] =
		(0.7 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.2 * $player['WINGERSKILL'] +
		0.05 * $player['PASSINGSKILL'] +
		0.05 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['WMDSkill'] =
		(0.3 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.1 * $player['PASSINGSKILL'] +
		0.4 * $player['WINGERSKILL'] +
		0.2 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['WMNSkill'] =
		(0.3 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.1 * $player['PASSINGSKILL'] +
		0.5 * $player['WINGERSKILL'] +
		0.1 * $player['DEFENDERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['WMOSkill'] =
		(0.3 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.1 * $player['PASSINGSKILL'] +
		0.6 * $player['WINGERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['WMCSkill'] =
		(0.6 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.2 * $player['PASSINGSKILL'] +
		0.2 * $player['WINGERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['ADSkill'] =
		(0.15 * ((0.35 + 0.13 * $player['STAMINASKILL'])) *
		$player['PLAYMAKERSKILL'] +
		(0.25 * $player['PASSINGSKILL'] +
		0.6 * $player['SCORERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['ANSkill'] =
		((0.25 * $player['PASSINGSKILL'] +
		0.75 * $player['SCORERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['AESkill'] =
		((0.5 * $player['WINGERSKILL'] +
		0.5 * $player['SCORERSKILL']) *
		(0.975 + 0.005 * $player['STAMINASKILL']));
	$player['Captain'] =
		(0.5 * $player['LEADERSHIP'] +
		0.5 * $player['EXPERIENCE']);

	$player['MaxValue'] = 0;
	$player['GKValue'] = ($player['OASkill'] * $player['GKSkill']);
	if ($player['GKValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['GKValue'];
	$player['CDNValue'] = ($player['OASkill'] * $player['CDNSkill']);
	if ($player['CDNValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['CDNValue'];
	$player['CDOValue'] = ($player['OASkill'] * $player['CDOSkill']);
	if ($player['CDOValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['CDOValue'];
	$player['CDEValue'] = ($player['OASkill'] * $player['CDESkill']);
	if ($player['CDEValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['CDEValue'];
	$player['WBDValue'] = ($player['OASkill'] * $player['WBDSkill']);
	if ($player['WBDValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['WBDValue'];
	$player['WBNValue'] = ($player['OASkill'] * $player['WBNSkill']);
	if ($player['WBNValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['WBNValue'];
	$player['WBOValue'] = ($player['OASkill'] * $player['WBOSkill']);
	if ($player['WBOValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['WBOValue'];
	$player['WBCValue'] = ($player['OASkill'] * $player['WBCSkill']);
	if ($player['WBCValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['WBCValue'];
	$player['MFDValue'] = ($player['OASkill'] * $player['MFDSkill']);
	if ($player['MFDValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['MFDValue'];
	$player['MFNValue'] = ($player['OASkill'] * $player['MFNSkill']);
	if ($player['MFNValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['MFNValue'];
	$player['MFOValue'] = ($player['OASkill'] * $player['MFOSkill']);
	if ($player['MFOValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['MFOValue'];
	$player['MFEValue'] = ($player['OASkill'] * $player['MFESkill']);
	if ($player['MFEValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['MFEValue'];
	$player['WMDValue'] = ($player['OASkill'] * $player['WMDSkill']);
	if ($player['WMDValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['WMDValue'];
	$player['WMNValue'] = ($player['OASkill'] * $player['WMNSkill']);
	if ($player['WMNValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['WMNValue'];
	$player['WMOValue'] = ($player['OASkill'] * $player['WMOSkill']);
	if ($player['WMOValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['WMOValue'];
	$player['WMCValue'] = ($player['OASkill'] * $player['WMCSkill']);
	if ($player['WMCValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['WMCValue'];
	$player['ADValue'] = ($player['OASkill'] * $player['ADSkill']);
	if ($player['ADValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['ADValue'];
	$player['ANValue'] = ($player['OASkill'] * $player['ANSkill']);
	if ($player['ANValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['ANValue'];
	$player['AEValue'] = ($player['OASkill'] * $player['AESkill']);
	if ($player['AEValue'] > $player['MaxValue'])
		$player['MaxValue'] = $player['AEValue'];
	return($player);
}

/**
 * Evaluate each player in every role.
 *
 * @access   public
 * @param    array   Players
 */
function evaluatePlayers($players)
{
	for ($i = 0; $i < count($players['PLAYER']); $i++)
		$players['PLAYER'][$i] = $this->evaluatePlayer($players['PLAYER'][$i]);
	return($players);
}

/**
 * Detect if player identified by provided PlayerID was kicked-out.
 *
 * @access   public
 * @param    string   PlayerID
 */
function isResigned($playerid)
{
	$transfer = $this->getTransferHistory($playerid, 'player');
	if (! $transfer)
		return false;
	if (strlen($transfer['PLAYERNAME']) > 0)
		return false;
	else return true;
}

/**
 * Check for missing players that went in the provided array but are not
 * available in the actual Hattrick DB.
 *
 * @access   public
 * @param    array   Old players list
 */
function getMissingPlayers($players)
{
	$htplayers = $this->getPlayers();
	if (! $htplayers)
		return $players;
	$this->playersMissing = array();
	for ($i = 0; $i < count($players['PLAYER']); $i++) {
		if (! $this->getPlayersItem($players['PLAYER'][$i]['PLAYERID'], $htplayers, 'PLAYERID'))
			array_push($this->playersMissing, $players['PLAYER'][$i]);
	}
	return $this->playersMissing;
}

/**
 * Check for changes in any players value. Calling this function will update
 * the players list in cache, adding some fields that could be checked to
 * see old values.
 *
 * @access   public
 * @param    array   Old players list
 * @param    boolean Insert old values and differences with actual ones.
 */
function getChanges($players, $fix = false)
{
	$missing = $this->getMissingPlayers($players);
	$htplayers = $this->getPlayers();
	if (! $htplayers)
		return false;
	for ($i = 0; $i < count($htplayers['PLAYER']); $i++) {
		$oldplayer = $this->getPlayersItem($htplayers['PLAYER'][$i]['PLAYERID'], $players, 'PLAYERID');
		if ($oldplayer) {
			$htplayers['PLAYER'][$i]['StaminaTrainings'] = $oldplayer['StaminaTrainings'];
			$htplayers['PLAYER'][$i]['KeeperTrainings'] = $oldplayer['KeeperTrainings'];
			$htplayers['PLAYER'][$i]['PlaymakerTrainings'] = $oldplayer['PlaymakerTrainings'];
			$htplayers['PLAYER'][$i]['ScorerTrainings'] = $oldplayer['ScorerTrainings'];
			$htplayers['PLAYER'][$i]['PassingTrainings'] = $oldplayer['PassingTrainings'];
			$htplayers['PLAYER'][$i]['WingerTrainings'] = $oldplayer['WingerTrainings'];
			$htplayers['PLAYER'][$i]['DefenderTrainings'] = $oldplayer['DefenderTrainings'];
			$htplayers['PLAYER'][$i]['SetPiecesTrainings'] = $oldplayer['SetPiecesTrainings'];
			if ($oldplayer['AGE'] != $htplayers['PLAYER'][$i]['AGE']) {
				$htplayers['PLAYER'][$i]['OldAGE'] = $oldplayer['AGE'];
				$htplayers['PLAYER'][$i]['DiffAGE'] = $htplayers['PLAYER'][$i]['AGE'] - $oldplayer['AGE'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffAGE'] > 0)
						$htplayers['PLAYER'][$i]['DiffAGE'] = '+'.$htplayers['PLAYER'][$i]['DiffAGE'];
					else if ($htplayers['PLAYER'][$i]['DiffAGE'] > 0)
						$htplayers['PLAYER'][$i]['DiffAGE'] = ' ';
				}
			}
			if ($oldplayer['MARKETVALUE'] != $htplayers['PLAYER'][$i]['MARKETVALUE']) {
				$htplayers['PLAYER'][$i]['OldMARKETVALUE'] = $oldplayer['MARKETVALUE'];
				$htplayers['PLAYER'][$i]['DiffMARKETVALUE'] = $htplayers['PLAYER'][$i]['MARKETVALUE'] - $oldplayer['MARKETVALUE'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffMARKETVALUE'] > 0)
						$htplayers['PLAYER'][$i]['DiffMARKETVALUE'] = '+'.$htplayers['PLAYER'][$i]['DiffMARKETVALUE'];
					else if ($htplayers['PLAYER'][$i]['DiffMARKETVALUE'] > 0)
						$htplayers['PLAYER'][$i]['DiffMARKETVALUE'] = ' ';
				}
			}
			if ($oldplayer['TSI'] != $htplayers['PLAYER'][$i]['TSI']) {
				$htplayers['PLAYER'][$i]['OldTSI'] = $oldplayer['TSI'];
				$htplayers['PLAYER'][$i]['DiffTSI'] = $htplayers['PLAYER'][$i]['TSI'] - $oldplayer['TSI'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffTSI'] > 0)
						$htplayers['PLAYER'][$i]['DiffTSI'] = '+'.$htplayers['PLAYER'][$i]['DiffTSI'];
					else if ($htplayers['PLAYER'][$i]['DiffTSI'] > 0)
						$htplayers['PLAYER'][$i]['DiffTSI'] = ' ';
				}
			}
			if ($oldplayer['PLAYERFORM'] != $htplayers['PLAYER'][$i]['PLAYERFORM']) {
				$htplayers['PLAYER'][$i]['OldPLAYERFORM'] = $oldplayer['PLAYERFORM'];
				$htplayers['PLAYER'][$i]['DiffPLAYERFORM'] = $htplayers['PLAYER'][$i]['PLAYERFORM'] - $oldplayer['PLAYERFORM'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffPLAYERFORM'] > 0)
						$htplayers['PLAYER'][$i]['DiffPLAYERFORM'] = '+'.$htplayers['PLAYER'][$i]['DiffPLAYERFORM'];
					else if ($htplayers['PLAYER'][$i]['DiffPLAYERFORM'] > 0)
						$htplayers['PLAYER'][$i]['DiffPLAYERFORM'] = ' ';
				}
			}
			if ($oldplayer['EXPERIENCE'] != $htplayers['PLAYER'][$i]['EXPERIENCE']) {
				$htplayers['PLAYER'][$i]['OldEXPERIENCE'] = $oldplayer['EXPERIENCE'];
				$htplayers['PLAYER'][$i]['DiffEXPERIENCE'] = $htplayers['PLAYER'][$i]['EXPERIENCE'] - $oldplayer['EXPERIENCE'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffEXPERIENCE'] > 0)
						$htplayers['PLAYER'][$i]['DiffEXPERIENCE'] = '+'.$htplayers['PLAYER'][$i]['DiffEXPERIENCE'];
					else if ($htplayers['PLAYER'][$i]['DiffEXPERIENCE'] > 0)
						$htplayers['PLAYER'][$i]['DiffEXPERIENCE'] = ' ';
				}
			}
			if ($oldplayer['LEADERSHIP'] != $htplayers['PLAYER'][$i]['LEADERSHIP']) {
				$htplayers['PLAYER'][$i]['OldLEADERSHIP'] = $oldplayer['LEADERSHIP'];
				$htplayers['PLAYER'][$i]['DiffLEADERSHIP'] = $htplayers['PLAYER'][$i]['LEADERSHIP'] - $oldplayer['LEADERSHIP'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffLEADERSHIP'] > 0)
						$htplayers['PLAYER'][$i]['DiffLEADERSHIP'] = '+'.$htplayers['PLAYER'][$i]['DiffLEADERSHIP'];
					else if ($htplayers['PLAYER'][$i]['DiffLEADERSHIP'] > 0)
						$htplayers['PLAYER'][$i]['DiffLEADERSHIP'] = ' ';
				}
			}
			if ($oldplayer['SALARY'] != $htplayers['PLAYER'][$i]['SALARY']) {
				$htplayers['PLAYER'][$i]['OldSALARY'] = $oldplayer['SALARY'];
				$htplayers['PLAYER'][$i]['DiffSALARY'] = $htplayers['PLAYER'][$i]['SALARY'] - $oldplayer['SALARY'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffSALARY'] > 0)
						$htplayers['PLAYER'][$i]['DiffSALARY'] = '+'.$htplayers['PLAYER'][$i]['DiffSALARY'];
					else if ($htplayers['PLAYER'][$i]['DiffSALARY'] > 0)
						$htplayers['PLAYER'][$i]['DiffSALARY'] = ' ';
				}
			}
			if ($oldplayer['LEAGUEGOALS'] != $htplayers['PLAYER'][$i]['LEAGUEGOALS']) {
				$htplayers['PLAYER'][$i]['OldLEAGUEGOALS'] = $oldplayer['LEAGUEGOALS'];
				$htplayers['PLAYER'][$i]['DiffLEAGUEGOALS'] = $htplayers['PLAYER'][$i]['LEAGUEGOALS'] - $oldplayer['LEAGUEGOALS'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffLEAGUEGOALS'] > 0)
						$htplayers['PLAYER'][$i]['DiffLEAGUEGOALS'] = '+'.$htplayers['PLAYER'][$i]['DiffLEAGUEGOALS'];
					else if ($htplayers['PLAYER'][$i]['DiffLEAGUEGOALS'] > 0)
						$htplayers['PLAYER'][$i]['DiffLEAGUEGOALS'] = ' ';
				}
			}
			if ($oldplayer['CUPGOALS'] != $htplayers['PLAYER'][$i]['CUPGOALS']) {
				$htplayers['PLAYER'][$i]['OldCUPGOALS'] = $oldplayer['CUPGOALS'];
				$htplayers['PLAYER'][$i]['DiffCUPGOALS'] = $htplayers['PLAYER'][$i]['CUPGOALS'] - $oldplayer['CUPGOALS'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffCUPGOALS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCUPGOALS'] = '+'.$htplayers['PLAYER'][$i]['DiffCUPGOALS'];
					else if ($htplayers['PLAYER'][$i]['DiffCUPGOALS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCUPGOALS'] = ' ';
				}
			}
			if ($oldplayer['FRIENDLYGOALS'] != $htplayers['PLAYER'][$i]['FRIENDLYGOALS']) {
				$htplayers['PLAYER'][$i]['OldFRIENDLYGOALS'] = $oldplayer['FRIENDLYGOALS'];
				$htplayers['PLAYER'][$i]['DiffFRIENDLYGOALS'] = $htplayers['PLAYER'][$i]['FRIENDLYGOALS'] - $oldplayer['FRIENDLYGOALS'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffFRIENDLYGOALS'] > 0)
						$htplayers['PLAYER'][$i]['DiffFRIENDLYGOALS'] = '+'.$htplayers['PLAYER'][$i]['DiffFRIENDLYGOALS'];
					else if ($htplayers['PLAYER'][$i]['DiffFRIENDLYGOALS'] > 0)
						$htplayers['PLAYER'][$i]['DiffFRIENDLYGOALS'] = ' ';
				}
			}
			if ($oldplayer['CAREERGOALS'] != $htplayers['PLAYER'][$i]['CAREERGOALS']) {
				$htplayers['PLAYER'][$i]['OldCAREERGOALS'] = $oldplayer['CAREERGOALS'];
				$htplayers['PLAYER'][$i]['DiffCAREERGOALS'] = $htplayers['PLAYER'][$i]['CAREERGOALS'] - $oldplayer['CAREERGOALS'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffCAREERGOALS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCAREERGOALS'] = '+'.$htplayers['PLAYER'][$i]['DiffCAREERGOALS'];
					else if ($htplayers['PLAYER'][$i]['DiffCAREERGOALS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCAREERGOALS'] = ' ';
				}
			}
			if ($oldplayer['CAREERHATTRICKS'] != $htplayers['PLAYER'][$i]['CAREERHATTRICKS']) {
				$htplayers['PLAYER'][$i]['OldCAREERHATTRICKS'] = $oldplayer['CAREERHATTRICKS'];
				$htplayers['PLAYER'][$i]['DiffCAREERHATTRICKS'] = $htplayers['PLAYER'][$i]['CAREERHATTRICKS'] - $oldplayer['CAREERHATTRICKS'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffCAREERHATTRICKS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCAREERHATTRICKS'] = '+'.$htplayers['PLAYER'][$i]['DiffCAREERHATTRICKS'];
					else if ($htplayers['PLAYER'][$i]['DiffCAREERHATTRICKS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCAREERHATTRICKS'] = ' ';
				}
			}
			if ($oldplayer['CAPS'] != $htplayers['PLAYER'][$i]['CAPS']) {
				$htplayers['PLAYER'][$i]['OldCAPS'] = $oldplayer['CAPS'];
				$htplayers['PLAYER'][$i]['DiffCAPS'] = $htplayers['PLAYER'][$i]['CAPS'] - $oldplayer['CAPS'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffCAPS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCAPS'] = '+'.$htplayers['PLAYER'][$i]['DiffCAPS'];
					else if ($htplayers['PLAYER'][$i]['DiffCAPS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCAPS'] = ' ';
				}
			}
			if ($oldplayer['CAPSU20'] != $htplayers['PLAYER'][$i]['CAPSU20']) {
				$htplayers['PLAYER'][$i]['OldCAPSU20'] = $oldplayer['CAPSU20'];
				$htplayers['PLAYER'][$i]['DiffCAPSU20'] = $htplayers['PLAYER'][$i]['CAPSU20'] - $oldplayer['CAPSU20'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffCAPSU20'] > 0)
						$htplayers['PLAYER'][$i]['DiffCAPSU20'] = '+'.$htplayers['PLAYER'][$i]['DiffCAPSU20'];
					else if ($htplayers['PLAYER'][$i]['DiffCAPSU20'] > 0)
						$htplayers['PLAYER'][$i]['DiffCAPSU20'] = ' ';
				}
			}
			if ($oldplayer['CARDS'] != $htplayers['PLAYER'][$i]['CARDS']) {
				$htplayers['PLAYER'][$i]['OldCARDS'] = $oldplayer['CARDS'];
				$htplayers['PLAYER'][$i]['DiffCARDS'] = $htplayers['PLAYER'][$i]['CARDS'] - $oldplayer['CARDS'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffCARDS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCARDS'] = '+'.$htplayers['PLAYER'][$i]['DiffCARDS'];
					else if ($htplayers['PLAYER'][$i]['DiffCARDS'] > 0)
						$htplayers['PLAYER'][$i]['DiffCARDS'] = ' ';
				}
			}
			if ($oldplayer['INJURYLEVEL'] != $htplayers['PLAYER'][$i]['INJURYLEVEL']) {
				$htplayers['PLAYER'][$i]['OldINJURYLEVEL'] = $oldplayer['INJURYLEVEL'];
				$htplayers['PLAYER'][$i]['DiffINJURYLEVEL'] = $htplayers['PLAYER'][$i]['INJURYLEVEL'] - $oldplayer['INJURYLEVEL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffINJURYLEVEL'] > 0)
						$htplayers['PLAYER'][$i]['DiffINJURYLEVEL'] = '+'.$htplayers['PLAYER'][$i]['DiffINJURYLEVEL'];
					else if ($htplayers['PLAYER'][$i]['DiffINJURYLEVEL'] > 0)
						$htplayers['PLAYER'][$i]['DiffINJURYLEVEL'] = ' ';
				}
			}
			if ($oldplayer['STAMINASKILL'] != $htplayers['PLAYER'][$i]['STAMINASKILL']) {
				$htplayers['PLAYER'][$i]['OldSTAMINASKILL'] = $oldplayer['STAMINASKILL'];
				$htplayers['PLAYER'][$i]['DiffSTAMINASKILL'] = $htplayers['PLAYER'][$i]['STAMINASKILL'] - $oldplayer['STAMINASKILL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffSTAMINASKILL'] > 0) {
						$htplayers['PLAYER'][$i]['DiffSTAMINASKILL'] = '+'.$htplayers['PLAYER'][$i]['DiffSTAMINASKILL'];
						$htplayers['PLAYER'][$i]['StaminaTrainings'] = 0;
					}
					else if ($htplayers['PLAYER'][$i]['DiffSTAMINASKILL'] > 0)
						$htplayers['PLAYER'][$i]['DiffSTAMINASKILL'] = ' ';
				}
			}
			if ($oldplayer['KEEPERSKILL'] != $htplayers['PLAYER'][$i]['KEEPERSKILL']) {
				$htplayers['PLAYER'][$i]['OldKEEPERSKILL'] = $oldplayer['KEEPERSKILL'];
				$htplayers['PLAYER'][$i]['DiffKEEPERSKILL'] = $htplayers['PLAYER'][$i]['KEEPERSKILL'] - $oldplayer['KEEPERSKILL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffKEEPERSKILL'] > 0) {
						$htplayers['PLAYER'][$i]['DiffKEEPERSKILL'] = '+'.$htplayers['PLAYER'][$i]['DiffKEEPERSKILL'];
						$htplayers['PLAYER'][$i]['KeeperTrainings'] = 0;
					}
					else if ($htplayers['PLAYER'][$i]['DiffKEEPERSKILL'] > 0)
						$htplayers['PLAYER'][$i]['DiffKEEPERSKILL'] = ' ';
				}
			}
			if ($oldplayer['PLAYMAKERSKILL'] != $htplayers['PLAYER'][$i]['PLAYMAKERSKILL']) {
				$htplayers['PLAYER'][$i]['OldPLAYMAKERSKILL'] = $oldplayer['PLAYMAKERSKILL'];
				$htplayers['PLAYER'][$i]['DiffPLAYMAKERSKILL'] = $htplayers['PLAYER'][$i]['PLAYMAKERSKILL'] - $oldplayer['PLAYMAKERSKILL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffPLAYMAKERSKILL'] > 0) {
						$htplayers['PLAYER'][$i]['DiffPLAYMAKERSKILL'] = '+'.$htplayers['PLAYER'][$i]['DiffPLAYMAKERSKILL'];
						$htplayers['PLAYER'][$i]['PlaymakerTrainings'] = 0;
					}
					else if ($htplayers['PLAYER'][$i]['DiffPLAYMAKERSKILL'] > 0)
						$htplayers['PLAYER'][$i]['DiffPLAYMAKERSKILL'] = ' ';
				}
			}
			if ($oldplayer['SCORERSKILL'] != $htplayers['PLAYER'][$i]['SCORERSKILL']) {
				$htplayers['PLAYER'][$i]['OldSCORERSKILL'] = $oldplayer['SCORERSKILL'];
				$htplayers['PLAYER'][$i]['DiffSCORERSKILL'] = $htplayers['PLAYER'][$i]['SCORERSKILL'] - $oldplayer['SCORERSKILL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffSCORERSKILL'] > 0) {
						$htplayers['PLAYER'][$i]['DiffSCORERSKILL'] = '+'.$htplayers['PLAYER'][$i]['DiffSCORERSKILL'];
						$htplayers['PLAYER'][$i]['ScorerTrainings'] = 0;
					}
					else if ($htplayers['PLAYER'][$i]['DiffSCORERSKILL'] > 0)
						$htplayers['PLAYER'][$i]['DiffSCORERSKILL'] = ' ';
				}
			}
			if ($oldplayer['PASSINGSKILL'] != $htplayers['PLAYER'][$i]['PASSINGSKILL']) {
				$htplayers['PLAYER'][$i]['OldPASSINGSKILL'] = $oldplayer['PASSINGSKILL'];
				$htplayers['PLAYER'][$i]['DiffPASSINGSKILL'] = $htplayers['PLAYER'][$i]['PASSINGSKILL'] - $oldplayer['PASSINGSKILL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffPASSINGSKILL'] > 0) {
						$htplayers['PLAYER'][$i]['DiffPASSINGSKILL'] = '+'.$htplayers['PLAYER'][$i]['DiffPASSINGSKILL'];
						$htplayers['PLAYER'][$i]['PassingTrainings'] = 0;
					}
					else if ($htplayers['PLAYER'][$i]['DiffPASSINGSKILL'] > 0)
						$htplayers['PLAYER'][$i]['DiffPASSINGSKILL'] = ' ';
				}
			}
			if ($oldplayer['WINGERSKILL'] != $htplayers['PLAYER'][$i]['WINGERSKILL']) {
				$htplayers['PLAYER'][$i]['OldWINGERSKILL'] = $oldplayer['WINGERSKILL'];
				$htplayers['PLAYER'][$i]['DiffWINGERSKILL'] = $htplayers['PLAYER'][$i]['WINGERSKILL'] - $oldplayer['WINGERSKILL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffWINGERSKILL'] > 0) {
						$htplayers['PLAYER'][$i]['DiffWINGERSKILL'] = '+'.$htplayers['PLAYER'][$i]['DiffWINGERSKILL'];
						$htplayers['PLAYER'][$i]['WingerTrainings'] = 0;
					}
					else if ($htplayers['PLAYER'][$i]['DiffWINGERSKILL'] > 0)
						$htplayers['PLAYER'][$i]['DiffWINGERSKILL'] = ' ';
				}
			}
			if ($oldplayer['DEFENDERSKILL'] != $htplayers['PLAYER'][$i]['DEFENDERSKILL']) {
				$htplayers['PLAYER'][$i]['OldDEFENDERSKILL'] = $oldplayer['DEFENDERSKILL'];
				$htplayers['PLAYER'][$i]['DiffDEFENDERSKILL'] = $htplayers['PLAYER'][$i]['DEFENDERSKILL'] - $oldplayer['DEFENDERSKILL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffDEFENDERSKILL'] > 0) {
						$htplayers['PLAYER'][$i]['DiffDEFENDERSKILL'] = '+'.$htplayers['PLAYER'][$i]['DiffDEFENDERSKILL'];
						$htplayers['PLAYER'][$i]['DefenderTrainings'] = 0;
					}
					else if ($htplayers['PLAYER'][$i]['DiffDEFENDERSKILL'] > 0)
						$htplayers['PLAYER'][$i]['DiffDEFENDERSKILL'] = ' ';
				}
			}
			if ($oldplayer['SETPIECESSKILL'] != $htplayers['PLAYER'][$i]['SETPIECESSKILL']) {
				$htplayers['PLAYER'][$i]['OldSETPIECESSKILL'] = $oldplayer['SETPIECESSKILL'];
				$htplayers['PLAYER'][$i]['DiffSETPIECESSKILL'] = $htplayers['PLAYER'][$i]['SETPIECESSKILL'] - $oldplayer['SETPIECESSKILL'];
				if ($fix) {
					if ($htplayers['PLAYER'][$i]['DiffSETPIECESSKILL'] > 0) {
						$htplayers['PLAYER'][$i]['DiffSETPIECESSKILL'] = '+'.$htplayers['PLAYER'][$i]['DiffSETPIECESSKILL'];
						$htplayers['PLAYER'][$i]['SetpiecesTrainings'] = 0;
					}
					else if ($htplayers['PLAYER'][$i]['DiffSETPIECESSKILL'] > 0)
						$htplayers['PLAYER'][$i]['DiffSETPIECESSKILL'] = ' ';
				}
			}
		}
	}
	$this->players = $htplayers;
	return $htplayers;
}

/**
 * Check for new players from the actual Hattrick DB, comparing it with
 * the provided array.
 *
 * @access   public
 * @param    array   Old players list
 */
function getNewPlayers($players)
{
	$htplayers = $this->getPlayers();
	if (! $htplayers)
		return false;
	$this->playersFound = array();
	for ($i = 0; $i < count($htplayers['PLAYER']); $i++) {
		$child = $this->getPlayersItem($htplayers['PLAYER'][$i]['PLAYERID'], $players, 'PLAYERID');
		if (! $child)
			array_push($this->playersFound, $htplayers['PLAYER'][$i]);
	}
	return $this->playersFound;
}

/**
 * Search for a player in an HTClient players or match lineup array.
 * If more than one player satisfies the query, returns the first occurency.
 *
 * @access   public
 * @param    string  Value to search for
 * @param    array   Players array, or the previoulsy retrieved one if not specified
 * @param    array   Field to compare, or PLAYERID if not specified
 */
function getPlayersItem($val, $players = false, $field = 'PLAYERID')
{

	if ($players == false)
		$players = $this->getPlayers();
	if ($players == false)
		return false;
	for ($i = 0; $i < count($players['PLAYER']); $i++) {
		if ($players['PLAYER'][$i][$field] == $val)
			return $players['PLAYER'][$i];
	}
	return false;
}

/**
 * Search for a team in an HTClient league array.
 * If more than one team satisfies the query, returns the first occurency.
 *
 * @access   public
 * @param    array   Value to search for
 * @param    array   League array, or the previoulsy retrieved one if not specified
 * @param    array   Field to compare, or TEAMID if not specified
 */
function getLeagueItem($val, $league = false, $field = 'TEAMID')
{
	if ($league == false)
		$league = $this->league;
	if ($league == false)
		return false;
	for ($i = 0; $i < count($league['TEAM']); $i++) {
		if ($league['TEAM'][$i][$field] == $val)
			return $league['TEAM'][$i];
	}
	return false;
}

/**
 * Search for a league in an HTClient worldLeagues array.
 * If more than one team satisfies the query, returns the first occurency.
 *
 * @access   public
 * @param    array   Value to search for
 * @param    array   worldLeagues array, or the previoulsy retrieved one if not specified
 * @param    array   Field to compare, or LEAGUEID if not specified
 */
function getWorldItem($val, $world = false, $field = 'LEAGUEID')
{
	if ($world == false)
		$world = $this->worldLeagues;
	if ($world == false)
		return false;
	for ($i = 0; $i < count($world['LEAGUE']); $i++) {
		if ($world['LEAGUE'][$i][$field] == $val)
			return $world['LEAGUE'][$i];
	}
	return false;
}

/**
 * Search for a match in an HTClient matches array.
 * If more than one player satisfies the query, returns the first occurency.
 *
 * @access   public
 * @param    string  Value to search for
 * @param    array   Matches array, or the previoulsy retrieved one if not specified
 * @param    array   Field to compare, or MATCHID if not specified
 */
function getMatchesItem($val, $matches = false, $field = 'MATCHID')
{

	if ($matches == false)
		$matches = $this->getMatches();
	if ($matches == false)
		return false;
	for ($i = 0; $i < count($matches['MATCH']); $i++) {
		if ($matches['MATCH'][$i][$field] == $val)
			return $matches['MATCH'][$i];
	}
	return false;
}

/**
 * Load full match details for every item in the provided matches array.
 * It can take a long time to complete, so use with caution.
 *
 * @access   public
 * @param    array   Matches
 */
function fillupMatches($matches)
{
	for ($i = 0; $i < count($matches['MATCH']); $i++) {
		$matches['MATCH'][$i]['Match'] = $this->getMatch($matches['MATCH'][$i]['MATCHID']);
		$matches['MATCH'][$i]['Lineup'] = $this->getMatchLineup($matches['MATCH'][$i]['MATCHID']);
	}
	return $matches;
}

/**
 * Courtesy of Wolfgang Schedl.
 * The function needs the copy&pasted Sourcecode of players.asp as only
 * value. It will return the player faces description in the form of an
 * array with the values: $arrayname[<playerid>][face|eye|nose|mouth].
 *
 * @access   public
 * @param    string   Copy&pasted Sourcecode of players.asp
 */
function insertPlayerImages($player_text)
{
	$prefix_face = "faces/";
	$prefix_eye = "eyes/";
	$prefix_nose = "noses/";
	$prefix_mouth = "mouths/";

	$search_prefix_ht_folder = "Images/Faces/([/:\-\'{}()._&a-zA-Z0-9+=]*)/faces/";
	$search_player_id = "playerID=([0-9]*)";
	$search_face = "faces/([/:\-\'{}()._&a-zA-Z0-9+=]*.gif)";
	$search_eye= "eyes/([/:\-\'{}()._&a-zA-Z0-9+=]*.gif)";
	$search_nose = "noses/([/:\-\'{}()._&a-zA-Z0-9+=]*.gif)";
	$search_mouth = "mouths/([/:\-\'{}()._&a-zA-Z0-9+=]*.gif)";

	$player_text_part = split("Images/Faces/bg.gif", $player_text);

	$i = 1;
	while($player_text_part[$i]) {
		if ( ereg($search_prefix_ht_folder, $player_text_part[$i], $imagepart) )
			$prefix_ht_folder = "$imagepart[1]";
		else return false;
		if ( ereg($search_player_id, $player_text_part[$i], $imagepart) )
			$player_id = "$imagepart[1]";
		else return false;
		if ( ereg($search_face, $player_text_part[$i], $imagepart) )
			$facefile = "$imagepart[1]";
		else return false;
		if ( ereg($search_eye, $player_text_part[$i], $imagepart) )
			$eyefile = "$imagepart[1]";
		if ( ereg($search_nose, $player_text_part[$i], $imagepart) )
			$nosefile = "$imagepart[1]";
		else return false;
		if ( ereg($search_mouth, $player_text_part[$i], $imagepart) )
			$mouthfile = "$imagepart[1]";
		else return false;

		$prefix_ht = "http://www.hattrick.org/common/Images/Faces/" . $prefix_ht_folder . "/";
		$face = $prefix_ht . $prefix_face . $facefile;
		$eye = $prefix_ht . $prefix_eye . $eyefile;
		$nose = $prefix_ht . $prefix_nose . $nosefile;
		$mouth = $prefix_ht . $prefix_mouth . $mouthfile;

		$faces[$player_id]['face'] = $face;
		$faces[$player_id]['eye'] = $eye;
		$faces[$player_id]['nose'] = $nose;
		$faces[$player_id]['mouth'] = $mouth;

		$i++;
        }
	return $faces;
}

/**
 * Courtesy of CHPP-ste1n.
 * Returns the LeagueID corrisponding to specified TeamID, without the
 * need to fetch any information form the Hattrick servers.
 * The TeamID list that is hard-coded inside this function needs to be
 * updated on every leagues expansion to get a full network-free feature,
 * but it will work anyway, fetching the new details when not listed.
 *
 * @access   public
 * @param    integer    TeamID
 */
function teamid2leagueid($teamid)
{
	if ($teamid >= 52 && $teamid <= 6023) return 1;
	if ($teamid >= 6024 && $teamid <= 6703) return 7;
	if ($teamid >= 6704 && $teamid <= 7383) return 3;
	if ($teamid >= 7384 && $teamid <= 8063) return 2;
	if ($teamid >= 8064 && $teamid <= 8743) return 8;
	if ($teamid >= 8744 && $teamid <= 8911) return 6;
	if ($teamid >= 8912 && $teamid <= 9079) return 5;
	if ($teamid >= 9080 && $teamid <= 9247) return 4;
	if ($teamid >= 9248 && $teamid <= 17439) return 1;
	if ($teamid >= 17440 && $teamid <= 20167) return 11;
	if ($teamid >= 20168 && $teamid <= 20847) return 9;
	if ($teamid >= 20848 && $teamid <= 21527) return 14;
	if ($teamid >= 21528 && $teamid <= 22207) return 12;
	if ($teamid >= 26840 && $teamid <= 27351) return 6;
	if ($teamid >= 27352 && $teamid <= 27391) return 29;
	if ($teamid >= 27392 && $teamid <= 27559) return 28;
	if ($teamid >= 28072 && $teamid <= 28239) return 32;
	if ($teamid >= 28240 && $teamid <= 28407) return 31;
	if ($teamid >= 28408 && $teamid <= 28575) return 30;
	if ($teamid >= 28576 && $teamid <= 28615) return 27;
	if ($teamid >= 28616 && $teamid <= 28783) return 26;
	if ($teamid >= 28784 && $teamid <= 28951) return 35;
	if ($teamid >= 28952 && $teamid <= 29119) return 15;
	if ($teamid >= 29120 && $teamid <= 29799) return 16;
	if ($teamid >= 29800 && $teamid <= 29967) return 17;
	if ($teamid >= 29968 && $teamid <= 30135) return 18;
	if ($teamid >= 30136 && $teamid <= 30140) return 34;
	if ($teamid >= 30141 && $teamid <= 30142) return 59;
	if ($teamid >= 30143 && $teamid <= 30153) return 34;
	if ($teamid >= 30154 && $teamid <= 30154) return 59;
	if ($teamid >= 30155 && $teamid <= 30160) return 34;
	if ($teamid >= 30161 && $teamid <= 30161) return 59;
	if ($teamid >= 30162 && $teamid <= 30162) return 34;
	if ($teamid >= 30163 && $teamid <= 30163) return 59;
	if ($teamid >= 30164 && $teamid <= 30177) return 34;
	if ($teamid >= 30178 && $teamid <= 30178) return 59;
	if ($teamid >= 30179 && $teamid <= 30188) return 34;
	if ($teamid >= 30189 && $teamid <= 30189) return 59;
	if ($teamid >= 30190 && $teamid <= 30192) return 34;
	if ($teamid >= 30193 && $teamid <= 30193) return 59;
	if ($teamid >= 30194 && $teamid <= 30194) return 34;
	if ($teamid >= 30195 && $teamid <= 30195) return 59;
	if ($teamid >= 30196 && $teamid <= 30199) return 34;
	if ($teamid >= 30200 && $teamid <= 30200) return 59;
	if ($teamid >= 30201 && $teamid <= 30204) return 34;
	if ($teamid >= 30205 && $teamid <= 30205) return 59;
	if ($teamid >= 30206 && $teamid <= 30208) return 34;
	if ($teamid >= 30209 && $teamid <= 30209) return 59;
	if ($teamid >= 30210 && $teamid <= 30212) return 34;
	if ($teamid >= 30213 && $teamid <= 30213) return 59;
	if ($teamid >= 30214 && $teamid <= 30219) return 34;
	if ($teamid >= 30220 && $teamid <= 30220) return 59;
	if ($teamid >= 30221 && $teamid <= 30279) return 34;
	if ($teamid >= 30280 && $teamid <= 30280) return 59;
	if ($teamid >= 30281 && $teamid <= 30303) return 34;
	if ($teamid >= 30304 && $teamid <= 30471) return 19;
	if ($teamid >= 30472 && $teamid <= 30511) return 33;
	if ($teamid >= 30512 && $teamid <= 31191) return 36;
	if ($teamid >= 31192 && $teamid <= 31871) return 20;
	if ($teamid >= 31872 && $teamid <= 32039) return 21;
	if ($teamid >= 32040 && $teamid <= 32207) return 22;
	if ($teamid >= 32208 && $teamid <= 32247) return 23;
	if ($teamid >= 32248 && $teamid <= 32927) return 24;
	if ($teamid >= 32928 && $teamid <= 33607) return 25;
	if ($teamid >= 33608 && $teamid <= 34119) return 31;
	if ($teamid >= 34120 && $teamid <= 36847) return 37;
	if ($teamid >= 36888 && $teamid <= 36927) return 38;
	if ($teamid >= 36928 && $teamid <= 36935) return 39;
	if ($teamid >= 36936 && $teamid <= 36943) return 46;
	if ($teamid >= 36976 && $teamid <= 36983) return 47;
	if ($teamid >= 36992 && $teamid <= 36999) return 45;
	if ($teamid >= 37000 && $teamid <= 37511) return 15;
	if ($teamid >= 37512 && $teamid <= 37677) return 47;
	if ($teamid >= 37678 && $teamid <= 37678) return 60;
	if ($teamid >= 37679 && $teamid <= 38183) return 47;
	if ($teamid >= 38184 && $teamid <= 38695) return 32;
	if ($teamid >= 38696 && $teamid <= 46887) return 1;
	if ($teamid >= 46888 && $teamid <= 47399) return 5;
	if ($teamid >= 47400 && $teamid <= 49447) return 36;
	if ($teamid >= 49448 && $teamid <= 52007) return 4;
	if ($teamid >= 52008 && $teamid <= 54055) return 3;
	if ($teamid >= 54056 && $teamid <= 64295) return 2;
	if ($teamid >= 64296 && $teamid <= 66343) return 9;
	if ($teamid >= 67497 && $teamid <= 67624) return 38;
	if ($teamid >= 67625 && $teamid <= 68136) return 26;
	if ($teamid >= 68137 && $teamid <= 70184) return 14;
	if ($teamid >= 70185 && $teamid <= 72232) return 8;
	if ($teamid >= 72233 && $teamid <= 72744) return 17;
	if ($teamid >= 72745 && $teamid <= 72904) return 46;
	if ($teamid >= 72905 && $teamid <= 73072) return 44;
	if ($teamid >= 73073 && $teamid <= 73232) return 45;
	if ($teamid >= 73233 && $teamid <= 73392) return 39;
	if ($teamid >= 73393 && $teamid <= 73904) return 21;
	if ($teamid >= 73905 && $teamid <= 75952) return 12;
	if ($teamid >= 75953 && $teamid <= 78000) return 15;
	if ($teamid >= 78001 && $teamid <= 78128) return 27;
	if ($teamid >= 78129 && $teamid <= 78256) return 33;
	if ($teamid >= 78257 && $teamid <= 80304) return 24;
	if ($teamid >= 80305 && $teamid <= 80816) return 39;
	if ($teamid >= 80817 && $teamid <= 81328) return 44;
	if ($teamid >= 81329 && $teamid <= 91568) return 25;
	if ($teamid >= 91569 && $teamid <= 93616) return 16;
	if ($teamid >= 93617 && $teamid <= 93784) return 52;
	if ($teamid >= 93785 && $teamid <= 93952) return 51;
	if ($teamid >= 93953 && $teamid <= 94120) return 50;
	if ($teamid >= 94121 && $teamid <= 94288) return 56;
	if ($teamid >= 94289 && $teamid <= 94456) return 58;
	if ($teamid >= 94457 && $teamid <= 94624) return 54;
	if ($teamid >= 94625 && $teamid <= 94792) return 55;
	if ($teamid >= 94793 && $teamid <= 94960) return 53;
	if ($teamid >= 94961 && $teamid <= 95128) return 57;
	if ($teamid >= 95129 && $teamid <= 95640) return 56;
	if ($teamid >= 95641 && $teamid <= 96152) return 51;
	if ($teamid >= 96153 && $teamid <= 96664) return 46;
	if ($teamid >= 96665 && $teamid <= 104856) return 1;
	if ($teamid >= 104857 && $teamid <= 106904) return 44;
	if ($teamid >= 106905 && $teamid <= 108952) return 17;
	if ($teamid >= 108953 && $teamid <= 111000) return 7;
	if ($teamid >= 111001 && $teamid <= 111128) return 29;
	if ($teamid >= 111129 && $teamid <= 111256) return 23;
	if ($teamid >= 111257 && $teamid <= 111262) return 34;
	if ($teamid >= 111263 && $teamid <= 111272) return 59;
	if ($teamid >= 111273 && $teamid <= 111278) return 34;
	if ($teamid >= 111279 && $teamid <= 111281) return 59;
	if ($teamid >= 111282 && $teamid <= 111283) return 34;
	if ($teamid >= 111284 && $teamid <= 111285) return 59;
	if ($teamid >= 111286 && $teamid <= 111286) return 34;
	if ($teamid >= 111287 && $teamid <= 111424) return 59;
	if ($teamid >= 111439 && $teamid <= 111440) return 60;
	if ($teamid >= 111441 && $teamid <= 111441) return 47;
	if ($teamid >= 111442 && $teamid <= 111606) return 60;
	if ($teamid >= 111607 && $teamid <= 112118) return 34;
	if ($teamid >= 112119 && $teamid <= 112630) return 59;
	if ($teamid >= 112631 && $teamid <= 112798) return 63;
	if ($teamid >= 112799 && $teamid <= 113310) return 54;
	if ($teamid >= 113311 && $teamid <= 113822) return 50;
	if ($teamid >= 113823 && $teamid <= 115870) return 56;
	if ($teamid >= 115871 && $teamid <= 116382) return 52;
	if ($teamid >= 116383 && $teamid <= 116894) return 53;
	if ($teamid >= 116895 && $teamid <= 117062) return 64;
	if ($teamid >= 117063 && $teamid <= 117230) return 62;
	if ($teamid >= 117231 && $teamid <= 117742) return 58;
	if ($teamid >= 117743 && $teamid <= 125934) return 36;
	if ($teamid >= 125935 && $teamid <= 134126) return 3;
	if ($teamid >= 134127 && $teamid <= 136174) return 46;
	if ($teamid >= 136175 && $teamid <= 136342) return 61;
	if ($teamid >= 136343 && $teamid <= 138390) return 47;
	if ($teamid >= 138391 && $teamid <= 138902) return 45;
	if ($teamid >= 138903 && $teamid <= 140950) return 31;
	if ($teamid >= 140951 && $teamid <= 142998) return 39;
	if ($teamid >= 142999 && $teamid <= 151190) return 14;
	if ($teamid >= 151191 && $teamid <= 151702) return 38;
	if ($teamid >= 151743 && $teamid <= 154302) return 63;
	if ($teamid >= 154303 && $teamid <= 154814) return 64;
	if ($teamid >= 154815 && $teamid <= 155326) return 62;
	if ($teamid >= 155327 && $teamid <= 157374) return 58;
	if ($teamid >= 157375 && $teamid <= 159422) return 51;
	if ($teamid >= 159423 && $teamid <= 167614) return 9;
	if ($teamid >= 167615 && $teamid <= 175806) return 46;
	if ($teamid >= 175807 && $teamid <= 177854) return 5;
	if ($teamid >= 177855 && $teamid <= 178366) return 61;
	if ($teamid >= 178367 && $teamid <= 178878) return 35;
	if ($teamid >= 178879 && $teamid <= 187070) return 37;
	if ($teamid >= 187071 && $teamid <= 195262) return 44;
	if ($teamid >= 195263 && $teamid <= 203454) return 11;
	if ($teamid >= 203455 && $teamid <= 205502) return 6;
	if ($teamid >= 205503 && $teamid <= 213694) return 14;
	if ($teamid >= 213695 && $teamid <= 221886) return 7;
	if ($teamid >= 221887 && $teamid <= 230078) return 8;
	if ($teamid >= 230079 && $teamid <= 230590) return 18;
	if ($teamid >= 230591 && $teamid <= 230758) return 70;
	if ($teamid >= 230759 && $teamid <= 232806) return 53;
	if ($teamid >= 232807 && $teamid <= 240998) return 4;
	if ($teamid >= 240999 && $teamid <= 241166) return 69;
	if ($teamid >= 241167 && $teamid <= 241334) return 66;
	if ($teamid >= 241335 && $teamid <= 241502) return 67;
	if ($teamid >= 241503 && $teamid <= 243550) return 26;
	if ($teamid >= 243551 && $teamid <= 251742) return 46;
	if ($teamid >= 251743 && $teamid <= 259934) return 12;
	if ($teamid >= 259935 && $teamid <= 260102) return 71;
	if ($teamid >= 260103 && $teamid <= 268294) return 24;
	if ($teamid >= 268295 && $teamid <= 268462) return 68;
	if ($teamid >= 268463 && $teamid <= 268974) return 28;
	if ($teamid >= 268975 && $teamid <= 269486) return 23;
	if ($teamid >= 269487 && $teamid <= 269998) return 19;
	if ($teamid >= 269999 && $teamid <= 272046) return 18;
	if ($teamid >= 272047 && $teamid <= 272558) return 29;
	if ($teamid >= 272687 && $teamid <= 273198) return 66;
	if ($teamid >= 273199 && $teamid <= 273710) return 57;
	if ($teamid >= 273711 && $teamid <= 275758) return 28;
	if ($teamid >= 275759 && $teamid <= 277806) return 19;
	if ($teamid >= 277807 && $teamid <= 279854) return 50;
	if ($teamid >= 279855 && $teamid <= 281902) return 21;
	if ($teamid >= 281911 && $teamid <= 281918) return 74;
	if ($teamid >= 281919 && $teamid <= 282118) return 73;
	if ($teamid >= 282119 && $teamid <= 282150) return 74;
	if ($teamid >= 282151 && $teamid <= 282158) return 77;
	if ($teamid >= 282159 && $teamid <= 282166) return 76;
	if ($teamid >= 282167 && $teamid <= 282206) return 75;
	if ($teamid >= 282207 && $teamid <= 282238) return 76;
	if ($teamid >= 282239 && $teamid <= 282270) return 77;
	if ($teamid >= 282271 && $teamid <= 282398) return 73;
	if ($teamid >= 282399 && $teamid <= 282526) return 74;
	if ($teamid >= 282527 && $teamid <= 282654) return 75;
	if ($teamid >= 282655 && $teamid <= 282782) return 76;
	if ($teamid >= 282783 && $teamid <= 282910) return 77;
	if ($teamid >= 282911 && $teamid <= 291102) return 18;
	if ($teamid >= 291103 && $teamid <= 299294) return 5;
	if ($teamid >= 299295 && $teamid <= 307486) return 39;
	if ($teamid >= 307487 && $teamid <= 315678) return 36;
	if ($teamid >= 315679 && $teamid <= 323870) return 44;
	if ($teamid >= 323871 && $teamid <= 332062) return 7;
	if ($teamid >= 332063 && $teamid <= 340254) return 3;
	if ($teamid >= 340255 && $teamid <= 340430) return 72;
	if ($teamid >= 340431 && $teamid <= 348622) return 63;
	if ($teamid >= 359503 && $teamid <= 367694) return 14;
	if ($teamid >= 392823 && $teamid <= 393334) return 74;
	if ($teamid >= 393335 && $teamid <= 393846) return 69;
	if ($teamid >= 393847 && $teamid <= 394358) return 76;
	if ($teamid >= 394359 && $teamid <= 394526) return 79;
	if ($teamid >= 394527 && $teamid <= 396574) return 32;
	if ($teamid >= 396575 && $teamid <= 398622) return 23;
	if ($teamid >= 398623 && $teamid <= 400670) return 45;
	if ($teamid >= 400671 && $teamid <= 408862) return 11;
	if ($teamid >= 408863 && $teamid <= 417054) return 56;
	if ($teamid >= 417055 && $teamid <= 425246) return 15;
	if ($teamid >= 425247 && $teamid <= 433438) return 16;
	if ($teamid >= 433439 && $teamid <= 433606) return 80;
	if ($teamid >= 433607 && $teamid <= 441798) return 36;
	if ($teamid >= 441799 && $teamid <= 449990) return 7;
	if ($teamid >= 449991 && $teamid <= 458182) return 1;
	if ($teamid >= 458223 && $teamid <= 458230) return 81;
	if ($teamid >= 458231 && $teamid <= 458238) return 83;
	if ($teamid >= 458239 && $teamid <= 458270) return 81;
	if ($teamid >= 458271 && $teamid <= 458302) return 83;
	if ($teamid >= 458303 && $teamid <= 458430) return 81;
	if ($teamid >= 458431 && $teamid <= 458558) return 83;
	if ($teamid >= 458559 && $teamid <= 460606) return 29;
	if ($teamid >= 460607 && $teamid <= 462654) return 54;
	if ($teamid >= 462655 && $teamid <= 462662) return 84;
	if ($teamid >= 462663 && $teamid <= 463174) return 33;
	if ($teamid >= 463175 && $teamid <= 463334) return 84;
	if ($teamid >= 463335 && $teamid <= 463502) return 85;
	if ($teamid >= 463503 && $teamid <= 463670) return 88;
	if ($teamid >= 463671 && $teamid <= 463838) return 89;
	if ($teamid >= 463839 && $teamid <= 472030) return 46;
	if ($teamid >= 472031 && $teamid <= 480222) return 24;
	if ($teamid >= 480223 && $teamid <= 482270) return 52;
	if ($teamid >= 482271 && $teamid <= 484318) return 66;
	if ($teamid >= 484359 && $teamid <= 484366) return 91;
	if ($teamid >= 484367 && $teamid <= 484374) return 97;
	if ($teamid >= 484375 && $teamid <= 484382) return 94;
	if ($teamid >= 484383 && $teamid <= 484390) return 95;
	if ($teamid >= 484391 && $teamid <= 484398) return 93;
	if ($teamid >= 484399 && $teamid <= 484406) return 96;
	if ($teamid >= 484407 && $teamid <= 484438) return 91;
	if ($teamid >= 484439 && $teamid <= 484470) return 97;
	if ($teamid >= 484471 && $teamid <= 484502) return 94;
	if ($teamid >= 484503 && $teamid <= 484534) return 95;
	if ($teamid >= 484535 && $teamid <= 484566) return 93;
	if ($teamid >= 484567 && $teamid <= 484726) return 96;
	if ($teamid >= 484727 && $teamid <= 484854) return 93;
	if ($teamid >= 484855 && $teamid <= 484982) return 95;
	if ($teamid >= 484983 && $teamid <= 485110) return 94;
	if ($teamid >= 485111 && $teamid <= 485238) return 91;
	if ($teamid >= 485239 && $teamid <= 485366) return 97;
	if ($teamid >= 485367 && $teamid <= 493558) return 36;
	if ($teamid >= 493559 && $teamid <= 501750) return 7;
	if ($teamid >= 501751 && $teamid <= 509942) return 14;
	if ($teamid >= 509943 && $teamid <= 518134) return 44;
	if ($teamid >= 518135 && $teamid <= 526326) return 3;
	if ($teamid >= 526327 && $teamid <= 526838) return 67;
	if ($teamid >= 526839 && $teamid <= 527350) return 22;
	if ($teamid >= 527351 && $teamid <= 527862) return 68;
	if ($teamid >= 527863 && $teamid <= 529910) return 64;
	if ($teamid >= 529911 && $teamid <= 531958) return 62;
	if ($teamid >= 531959 && $teamid <= 534006) return 74;
	if ($teamid >= 534007 && $teamid <= 536054) return 69;
	if ($teamid >= 536055 && $teamid <= 544246) return 19;
	if ($teamid >= 544247 && $teamid <= 552438) return 4;
	if ($teamid >= 552439 && $teamid <= 552950) return 27;
	if ($teamid >= 552951 && $teamid <= 561142) return 12;
	if ($teamid >= 561143 && $teamid <= 569334) return 25;
	if ($teamid >= 569335 && $teamid <= 577526) return 11;
	if ($teamid >= 577527 && $teamid <= 585718) return 51;
	if ($teamid >= 585719 && $teamid <= 593910) return 5;
	if ($teamid >= 612983 && $teamid <= 613494) return 72;
	if ($teamid >= 613495 && $teamid <= 614006) return 81;
	if ($teamid >= 614007 && $teamid <= 614518) return 70;
	if ($teamid >= 614519 && $teamid <= 615030) return 96;
	if ($teamid >= 615031 && $teamid <= 617078) return 35;
	if ($teamid >= 617079 && $teamid <= 619126) return 57;
	if ($teamid >= 619127 && $teamid <= 627318) return 6;
	if ($teamid >= 627319 && $teamid <= 635510) return 46;
	if ($teamid >= 635511 && $teamid <= 643702) return 28;
	if ($teamid >= 643703 && $teamid <= 651894) return 3;
	if ($teamid >= 651895 && $teamid <= 660086) return 32;
	if ($teamid >= 660087 && $teamid <= 668278) return 62;
	if ($teamid >= 668279 && $teamid <= 676470) return 66;
	if ($teamid >= 676471 && $teamid <= 684662) return 53;
	if ($teamid >= 684663 && $teamid <= 692854) return 23;
	if ($teamid >= 692855 && $teamid <= 701046) return 37;
	if ($teamid >= 701047 && $teamid <= 709238) return 18;
	if ($teamid >= 709239 && $teamid <= 709246) return 100;
	if ($teamid >= 709247 && $teamid <= 709254) return 99;
	if ($teamid >= 709255 && $teamid <= 709262) return 101;
	if ($teamid >= 709263 && $teamid <= 709270) return 103;
	if ($teamid >= 709271 && $teamid <= 709302) return 100;
	if ($teamid >= 709303 && $teamid <= 709334) return 99;
	if ($teamid >= 709335 && $teamid <= 709366) return 101;
	if ($teamid >= 709367 && $teamid <= 709398) return 103;
	if ($teamid >= 709399 && $teamid <= 709526) return 99;
	if ($teamid >= 709527 && $teamid <= 709654) return 100;
	if ($teamid >= 709655 && $teamid <= 709782) return 103;
	if ($teamid >= 709783 && $teamid <= 709910) return 101;
	if ($teamid >= 709911 && $teamid <= 709918) return 98;
	if ($teamid >= 709919 && $teamid <= 709958) return 102;
	if ($teamid >= 709959 && $teamid <= 710118) return 98;
	if ($teamid >= 710119 && $teamid <= 710246) return 102;
	if ($teamid >= 710247 && $teamid <= 710254) return 104;
	if ($teamid >= 710271 && $teamid <= 710278) return 105;
	if ($teamid >= 710279 && $teamid <= 710310) return 104;
	if ($teamid >= 710311 && $teamid <= 710318) return 106;
	if ($teamid >= 710319 && $teamid <= 710350) return 105;
	if ($teamid >= 710351 && $teamid <= 710510) return 106;
	if ($teamid >= 710511 && $teamid <= 710638) return 105;
	if ($teamid >= 710639 && $teamid <= 710766) return 104;
	if ($teamid >= 710767 && $teamid <= 710934) return 107;
	if ($teamid >= 710935 && $teamid <= 711446) return 93;
	if ($teamid >= 711447 && $teamid <= 713494) return 67;
	if ($teamid >= 713495 && $teamid <= 714006) return 101;
	if ($teamid >= 714007 && $teamid <= 714518) return 55;
	if ($teamid >= 714519 && $teamid <= 722710) return 16;
	if ($teamid >= 722711 && $teamid <= 730902) return 4;
	if ($teamid >= 730903 && $teamid <= 739094) return 25;
	if ($teamid >= 739095 && $teamid <= 747286) return 44;
	if ($teamid >= 747287 && $teamid <= 755478) return 46;
	if ($teamid >= 755479 && $teamid <= 763670) return 1;
	if ($teamid >= 763671 && $teamid <= 771862) return 14;
	if ($teamid >= 771863 && $teamid <= 780054) return 36;
	if ($teamid >= 780055 && $teamid <= 788246) return 12;
	if ($teamid >= 788247 && $teamid <= 788758) return 30;
	if ($teamid >= 788759 && $teamid <= 790806) return 34;
	if ($teamid >= 790807 && $teamid <= 791318) return 60;
	if ($teamid >= 791319 && $teamid <= 793366) return 59;
	if ($teamid >= 793367 && $teamid <= 793534) return 110;
	if ($teamid >= 793535 && $teamid <= 793702) return 111;
	if ($teamid >= 793703 && $teamid <= 793870) return 112;
	if ($teamid >= 793871 && $teamid <= 794038) return 113;
	if ($teamid >= 794039 && $teamid <= 802230) return 36;
	if ($teamid >= 802231 && $teamid <= 810422) return 46;
	if ($teamid >= 810423 && $teamid <= 818614) return 14;
	if ($teamid >= 818615 && $teamid <= 826806) return 3;
	if ($teamid >= 826807 && $teamid <= 834998) return 7;
	if ($teamid >= 834999 && $teamid <= 843190) return 44;
	if ($teamid >= 850067 && $teamid <= 858258) return 4;
	if ($teamid >= 858259 && $teamid <= 866450) return 25;
	if ($teamid >= 866451 && $teamid <= 874642) return 24;
	if ($teamid >= 874643 && $teamid <= 882834) return 37;
	if ($teamid >= 882835 && $teamid <= 891026) return 58;
	if ($teamid >= 891027 && $teamid <= 899218) return 52;
	if ($teamid >= 899219 && $teamid <= 907410) return 50;
	if ($teamid >= 907411 && $teamid <= 915602) return 29;
	if ($teamid >= 915603 && $teamid <= 923794) return 34;
	if ($teamid >= 923795 && $teamid <= 924306) return 91;
	if ($teamid >= 924307 && $teamid <= 924818) return 84;
	if ($teamid >= 924819 && $teamid <= 925330) return 89;
	if ($teamid >= 925331 && $teamid <= 925842) return 97;
	if ($teamid >= 925843 && $teamid <= 934034) return 11;
	if ($teamid >= 934035 && $teamid <= 936082) return 60;
	if ($teamid >= 936083 && $teamid <= 936594) return 103;
	if ($teamid >= 936595 && $teamid <= 937106) return 73;
	if ($teamid >= 937107 && $teamid <= 939154) return 96;
	if ($teamid >= 939155 && $teamid <= 941202) return 22;
	if ($teamid >= 941203 && $teamid <= 943250) return 30;
	if ($teamid >= 943251 && $teamid <= 951442) return 74;
	if ($teamid >= 951443 && $teamid <= 959634) return 2;
	if ($teamid >= 959635 && $teamid <= 967826) return 5;
	if ($teamid >= 967827 && $teamid <= 976018) return 18;
	if ($teamid >= 976019 && $teamid <= 984210) return 3;
	if ($teamid >= 984211 && $teamid <= 986258) return 68;
	if ($teamid >= 986259 && $teamid <= 986426) return 117;
	if ($teamid >= 986427 && $teamid <= 986594) return 118;
	if ($teamid >= 986595 && $teamid <= 986762) return 119;
	if ($teamid >= 986763 && $teamid <= 986930) return 120;
	if ($teamid >= 986931 && $teamid <= 986938) return 121;
	if ($teamid >= 986939 && $teamid <= 986946) return 122;
	if ($teamid >= 986947 && $teamid <= 986978) return 121;
	if ($teamid >= 986979 && $teamid <= 987010) return 122;
	if ($teamid >= 987011 && $teamid <= 987138) return 121;
	// not found, look up manually
	if ($teamdetails = $this->getTeamDetails($teamid))
		return $teamdetails['LEAGUEID'];
	else return false;
}

/**
 * Courtesy of CHPP-ste1n.
 * Returns an array that can be used to check how many flags has been
 * catched by the team with specified TeamID, both for home
 * ($flags['home']) and away ($flags['away']) matches.
 *
 * @access   public
 * @param    integer    TeamID
 */
function getFlags($teamid = false)
{
	if (! ($teamdetails = $this->getTeamDetails($teamid)))
		return false;
	$fromdate = $teamdetails['SIGNUPDATE'];
	$ownleague = $teamdetails['LEAGUEID'];
	$ownid = $teamdetails['TEAMID'];
	$matchesfetched = 0;
	$flags = array("home"=>array(), "away"=>array());
	do {
		$matchesarchive = $this->getMatchesArchive($ownid, false, $fromdate);
		if (($matchesfetched = count($matchesarchive['MATCH'])) == 0)
			return false;
		foreach ($matchesarchive['MATCH'] as $match) {
			if ( $match['MATCHTYPE'] == 4 ||
			$match['MATCHTYPE'] == 5 ||
			$match['MATCHTYPE'] == 8 ||
			$match['MATCHTYPE'] == 9) {
				if ($match['HOMETEAMID'] == $ownid) {
					$flags['home'][$this->teamid2leagueid($match['AWAYTEAMID'])]++;
				} else if ($match['AWAYTEAMID'] == $ownid) {
					$flags['away'][$this->teamid2leagueid($match['HOMETEAMID'])]++;
				}
			}
		}
		$fromdate = $matchesarchive['MATCH'][$matchesfetched-1]['MATCHDATE'];
	} while ($matchesfetched == 50);
	return $flags;
}

} ?>
